The Find the Number of Copy Arrays interview question gives you an array original and a 2D array bounds where bounds[i] = [lower_i, upper_i]. You need to find how many arrays copy exist such that:
copy[i] is within bounds[i].copy[i] - copy[i-1] == original[i] - original[i-1].Google uses this problem to test your ability to handle range intersections. The second condition implies that the entire copy array is determined once you pick the first element copy[0]. The problem then becomes: "What is the range of valid values for copy[0] such that all subsequent elements stay within their bounds?"
This is a Range Intersection problem.
[min_0, max_0], initially bounds[0].diff = original[i] - original[0].copy[i] will be copy[0] + diff.bounds[i].lower <= copy[0] + diff <= bounds[i].upper.copy[0]: bounds[i].lower - diff <= copy[0] <= bounds[i].upper - diff.[min_0, max_0].max(0, max_0 - min_0 + 1).original = [1, 2, 3], bounds = [[1, 5], [1, 5], [1, 5]].
copy[0] in [1, 5].diff = 2 - 1 = 1. copy[1] = copy[0] + 1.
diff = 3 - 1 = 2. copy[2] = copy[0] + 2.
copy[0] values: 1, 2, 3. Count = 3.i and i-1 instead of the cumulative difference from the start.When a sequence is constrained by fixed deltas, the entire sequence is a "shifted" version of the first element. This reduces a multi-variable problem to a single-variable range intersection problem. This is a very common Array interview pattern.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Beautiful Arrangement II | Medium | Solve | |
| Escape The Ghosts | Medium | Solve | |
| Maximum of Absolute Value Expression | Medium | Solve | |
| Number of Zero-Filled Subarrays | Medium | Solve | |
| Minimum Moves to Equal Array Elements | Medium | Solve |