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])for1 <= i <= n - 1u_i <= copy[i] <= v_ifor0 <= 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
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
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code