Solve the following four problems:
-
Count powers of k in an array: Given an array of positive integers nums and an integer k >= 1, return how many elements x in nums are exact powers of k (i.e., there exists t >= 0 with x = k^t). Handle the special case k = 1 (only x = 1 qualifies).
-
Compute a moon phase by date: On a planet where moon phases repeat every 8 days (phase indices 0..
7), you are given the phase index on day 1 of the year and the calendar for that year as a list of month lengths. Given a target date (month, day), compute the phase index on that date by converting the date to a day offset from day 1 and applying modulo 8. Be careful with month-length arithmetic and off-by-one errors.
-
Simulate bubble explosions with gravity: Given an R x C grid of integers representing colors and a threshold T (e.g., T =
3), repeatedly remove any 4-directionally connected component of size >= T consisting of the same color by setting its cells to 0; after each removal, apply gravity within each column so cells fall to the lowest available positions. Continue until no more removals are possible and return the final grid.
-
Count near-equal pairs via at most two swaps: You are given an array of N numeric strings of equal length L (leading zeros allowed). Count unordered pairs (i, j) such that s[i] can be transformed into s[j] using at most two swaps of characters within a single string. Clearly define the transformation rule, state necessary conditions (e.g., matching digit multisets), and design an efficient algorithm with time and space complexity analysis.