Find the Number of Copy Arrays - Problem

You are given an array original of length n and a 2D array bounds of length n × 2, where bounds[i] = [u_i, v_i].

You need to find the number of possible arrays copy of length n such that:

  • (copy[i] - copy[i - 1]) == (original[i] - original[i - 1]) for 1 <= i <= n - 1
  • u_i <= copy[i] <= v_i for 0 <= i <= n - 1

Return the number of such arrays.

Input & Output

Example 1 — Basic Case
$ Input: original = [1,3,2], bounds = [[1,1],[1,3],[2,4]]
Output: 1
💡 Note: Only one valid copy array [1,3,2]. Starting with copy[0]=1, we get copy[1]=1+(3-1)=3, copy[2]=3+(2-3)=2. All elements satisfy their bounds.
Example 2 — No Valid Arrays
$ Input: original = [1,2,3], bounds = [[1,1],[2,2],[5,5]]
Output: 0
💡 Note: If copy[0]=1, then copy[1]=1+(2-1)=2, copy[2]=2+(3-2)=3. But copy[2]=3 doesn't satisfy bounds[2]=[5,5], so no valid arrays exist.
Example 3 — Multiple Valid Arrays
$ Input: original = [1,1,1], bounds = [[1,3],[1,3],[1,3]]
Output: 3
💡 Note: Since all differences are 0, copy array will be [x,x,x] for any starting value x. Valid starting values are 1,2,3, giving 3 possible arrays.

Constraints

  • 1 ≤ n ≤ 105
  • -109 ≤ original[i] ≤ 109
  • -109 ≤ ui ≤ vi ≤ 109

Visualization

Tap to expand
Find Number of Copy Arrays132Original Array[1,1][1,3][2,4]Bounds132Valid Copy ArraySame differencesDifferences: +2, -1copy[1] = copy[0] + 2, copy[2] = copy[1] - 1Answer: 1 valid starting value
Understanding the Visualization
1
Input
Original array and bounds for each position
2
Constraint
Copy must have same differences but fit in bounds
3
Count
Number of valid starting values
Key Takeaway
🎯 Key Insight: Once the first element is determined, the entire copy sequence follows from the original's differences
Asked in
Google 15 Microsoft 12
8.5K Views
Medium Frequency
~25 min Avg. Time
350 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen