Widest Pair of Indices With Equal Range Sum - Problem
You are given two 0-indexed binary arrays nums1 and nums2.
Find the widest pair of indices (i, j) such that i <= j and nums1[i] + nums1[i+1] + ... + nums1[j] == nums2[i] + nums2[i+1] + ... + nums2[j].
The widest pair of indices is the pair with the largest distance between i and j. The distance between a pair of indices is defined as j - i + 1.
Return the distance of the widest pair of indices. If no pair of indices meets the conditions, return 0.
Input & Output
Example 1 — Basic Case
$
Input:
nums1 = [1,0,1,1,0], nums2 = [0,1,1,1,0]
›
Output:
3
💡 Note:
The widest pair is (2,4) where both arrays have sum 2 in range [2,4]. Distance = 4-2+1 = 3.
Example 2 — Equal Arrays
$
Input:
nums1 = [1,1,0], nums2 = [1,1,0]
›
Output:
3
💡 Note:
Arrays are identical, so entire range [0,2] has equal sums. Distance = 2-0+1 = 3.
Example 3 — No Valid Range
$
Input:
nums1 = [1,0], nums2 = [0,1]
›
Output:
0
💡 Note:
No range has equal sums: [0,0] has 1≠0, [1,1] has 0≠1, [0,1] has 1≠1.
Constraints
- 1 ≤ nums1.length, nums2.length ≤ 105
- nums1.length == nums2.length
- nums1[i], nums2[i] ∈ {0, 1}
Visualization
Tap to expand
Understanding the Visualization
1
Input Arrays
Two binary arrays nums1 and nums2 of equal length
2
Find Equal Sums
Look for ranges [i,j] where sum(nums1[i:j]) = sum(nums2[i:j])
3
Maximum Distance
Return the largest distance j-i+1 among valid ranges
Key Takeaway
🎯 Key Insight: Transform to difference array and find longest zero-sum subarray using prefix sums
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code