Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
You are given an array original of length n and a 2D array bounds of length n x 2, where bounds[i] = [ui, vi].
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.ui <= copy[i] <= vi for 0 <= i <= n - 1.Return the number of such arrays.
Example 1:
Input: original = [1,2,3,4], bounds = [[1,2],[2,3],[3,4],[4,5]]
Output: 2
Explanation:
The possible arrays are:
[1, 2, 3, 4][2, 3, 4, 5]Example 2:
Input: original = [1,2,3,4], bounds = [[1,10],[2,9],[3,8],[4,7]]
Output: 4
Explanation:
The possible arrays are:
[1, 2, 3, 4][2, 3, 4, 5][3, 4, 5, 6][4, 5, 6, 7]Example 3:
Input: original = [1,2,1,2], bounds = [[1,1],[2,3],[3,3],[2,3]]
Output: 0
Explanation:
No array is possible.
Constraints:
2 <= n == original.length <= 1051 <= original[i] <= 109bounds.length == nbounds[i].length == 21 <= bounds[i][0] <= bounds[i][1] <= 109Problem summary: You are given an array original of length n and a 2D array bounds of length n x 2, where bounds[i] = [ui, vi]. 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. ui <= copy[i] <= vi for 0 <= i <= n - 1. Return the number of such arrays.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Math
[1,2,3,4] [[1,2],[2,3],[3,4],[4,5]]
[1,2,3,4] [[1,10],[2,9],[3,8],[4,7]]
[1,2,1,2] [[1,1],[2,3],[3,3],[2,3]]
count-of-range-sum)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #3468: Find the Number of Copy Arrays
class Solution {
public int countArrays(int[] original, int[][] bounds) {
int mn = bounds[0][0];
int mx = bounds[0][1];
for (int i = 1; i < original.length; ++i) {
final int diff = original[i] - original[i - 1];
mn = Math.max(mn + diff, bounds[i][0]);
mx = Math.min(mx + diff, bounds[i][1]);
}
return Math.max(0, mx - mn + 1);
}
}
// Accepted solution for LeetCode #3468: Find the Number of Copy Arrays
package main
import "math"
// https://space.bilibili.com/206214
func countArrays(original []int, bounds [][]int) int {
mn, mx := math.MinInt, math.MaxInt
for i, b := range bounds {
mn = max(mn, b[0]-original[i]) // 计算区间交集
mx = min(mx, b[1]-original[i])
}
return max(mx-mn+1, 0) // 注意交集可能是空的
}
# Accepted solution for LeetCode #3468: Find the Number of Copy Arrays
class Solution:
def countArrays(self, original: list[int], bounds: list[list[int]]) -> int:
mn, mx = bounds[0]
for i in range(1, len(original)):
diff = original[i] - original[i - 1]
mn = max(mn + diff, bounds[i][0])
mx = min(mx + diff, bounds[i][1])
return max(0, mx - mn + 1)
// Accepted solution for LeetCode #3468: Find the Number of Copy Arrays
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #3468: Find the Number of Copy Arrays
// class Solution {
// public int countArrays(int[] original, int[][] bounds) {
// int mn = bounds[0][0];
// int mx = bounds[0][1];
//
// for (int i = 1; i < original.length; ++i) {
// final int diff = original[i] - original[i - 1];
// mn = Math.max(mn + diff, bounds[i][0]);
// mx = Math.min(mx + diff, bounds[i][1]);
// }
//
// return Math.max(0, mx - mn + 1);
// }
// }
// Accepted solution for LeetCode #3468: Find the Number of Copy Arrays
// Auto-generated TypeScript example from java.
function exampleSolution(): void {
}
// Reference (java):
// // Accepted solution for LeetCode #3468: Find the Number of Copy Arrays
// class Solution {
// public int countArrays(int[] original, int[][] bounds) {
// int mn = bounds[0][0];
// int mx = bounds[0][1];
//
// for (int i = 1; i < original.length; ++i) {
// final int diff = original[i] - original[i - 1];
// mn = Math.max(mn + diff, bounds[i][0]);
// mx = Math.min(mx + diff, bounds[i][1]);
// }
//
// return Math.max(0, mx - mn + 1);
// }
// }
Use this to step through a reusable interview workflow for this problem.
Two nested loops check every pair or subarray. The outer loop fixes a starting point, the inner loop extends or searches. For n elements this gives up to n²/2 operations. No extra space, but the quadratic time is prohibitive for large inputs.
Most array problems have an O(n²) brute force (nested loops) and an O(n) optimal (single pass with clever state tracking). The key is identifying what information to maintain as you scan: a running max, a prefix sum, a hash map of seen values, or two pointers.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.