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 i in the range [l, r], you pick either nums1[i] or nums2[i].
  • The sum of the numbers you pick from nums1 equals the sum of the numbers you pick from nums2 (the sum is considered to be 0 if 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 != l2
  • r1 != r2
  • nums1[i] is picked in the first range, and nums2[i] is picked in the second range or vice versa for at least one i.

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
Choose Numbers From Two Arrays in Range INPUT nums1: 1 2 3 i=0 i=1 i=2 nums2: 4 5 6 diff[i] = nums1[i] - nums2[i] -3 -3 -3 Range: [l, r] where 0 <= l <= r < n Goal: Find balanced ranges sum(picked from nums1) = sum(picked from nums2) ALGORITHM STEPS 1 Transform to Diff Array Pick nums1[i] --> +diff[i] Pick nums2[i] --> -diff[i] 2 DP with Offset Map dp[sum] = count of ways to reach that sum 3 For each position i Update DP: add +d or -d Count ranges ending at i 4 Count sum=0 cases Each dp[0] contributes to balanced ranges DP State Transitions: l=0,r=0: pick 1 or 4 l=1,r=1: pick 2 or 5 l=2,r=2: pick 3 or 6 l=0,r=2: all combinations Result: mod 10^9+7 FINAL RESULT 4 Balanced Ranges Found: Range [0,2]: Pick: 1+2+3=6 vs 4+5-3=6 Range [0,2]: Pick: 1+5+6=12 vs 2+3+4+3 Range [0,2]: Other valid combination Range [0,2]: 4th valid combination OUTPUT 4 Key Insight: Transform the problem: Instead of tracking two sums, use difference d[i] = nums1[i] - nums2[i]. A range is balanced when the sum of differences equals 0. Use DP with hash map to track all possible sum values and count ways to achieve sum=0 for each ending position. Time: O(n * range), Space: O(range) TutorialsPoint - Choose Numbers From Two Arrays in Range | Optimized DP with Difference Array
Asked in
Google 15 Meta 12 Amazon 8
8.5K Views
Medium Frequency
~35 min Avg. Time
245 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