Choose Numbers From Two Arrays in Range - Problem
You are given two 0-indexed integer arrays nums1 and nums2 of length n.
A range [l, r] (inclusive) where 0 <= l <= r < n is balanced if:
- For every
iin the range[l, r], you pick eithernums1[i]ornums2[i]. - The sum of the numbers you pick from
nums1equals the sum of the numbers you pick fromnums2(the sum is considered to be0if you pick no numbers from an array).
Two balanced ranges [l1, r1] and [l2, r2] are considered to be different if at least one of the following is true:
l1 != l2r1 != r2nums1[i]is picked in the first range, andnums2[i]is picked in the second range or vice versa for at least onei.
Return the number of different ranges that are balanced. Since the answer may be very large, return it modulo 10⁹ + 7.
Input & Output
Example 1 — Basic Case
$
Input:
nums1 = [1,2,3], nums2 = [4,5,6]
›
Output:
0
💡 Note:
For nums1=[1,2,3], nums2=[4,5,6], checking all ranges: Range [0,0] has 2 selections with sums (1,0) and (0,4) - not balanced. Range [1,1] has 2 selections with sums (2,0) and (0,5) - not balanced. Range [2,2] has 2 selections with sums (3,0) and (0,6) - not balanced. Range [0,1] has 4 selections with sums (3,0), (1,5), (2,4), (0,9) - not balanced. Range [1,2] has 4 selections with sums (5,0), (2,6), (3,5), (0,11) - not balanced. Range [0,2] has 8 selections with sums (6,0), (3,6), (4,5), (1,11), (5,4), (2,10), (3,9), (0,15) - not balanced. Total: 0 balanced ranges.
Example 2 — Equal Arrays
$
Input:
nums1 = [1,1], nums2 = [1,1]
›
Output:
2
💡 Note:
For nums1=[1,1], nums2=[1,1]: Range [0,0] has selections (1,0) and (0,1) - not balanced. Range [1,1] has selections (1,0) and (0,1) - not balanced. Range [0,1] has selections (2,0), (1,1), (1,1), (0,2) where two selections give sum1=1, sum2=1 - these are balanced. Total: 2 balanced ranges.
Example 3 — Single Element
$
Input:
nums1 = [5], nums2 = [5]
›
Output:
0
💡 Note:
For nums1=[5], nums2=[5]: Only range [0,0] with selections (5,0) and (0,5) - neither balanced. Total: 0 balanced ranges.
Constraints
- 1 ≤ n ≤ 100
- -1000 ≤ nums1[i], nums2[i] ≤ 1000
- Answer fits in 32-bit integer after modulo
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code