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 to 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 from [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 ofdifferent ranges that are balanced. _Since the answer may be very large, return it modulo109 + 7.
Input: nums1 =[1,2,5], nums2 =[2,6,3]Output: 3Explanation: The balanced ranges are:-[0,1] where we choose nums2[0], and nums1[1]. The sum of the numbers chosen from nums1 equals the sum of the numbers chosen from nums2:2=2.-[0,2] where we choose nums1[0], nums2[1], and nums1[2]. The sum of the numbers chosen from nums1 equals the sum of the numbers chosen from nums2:1+5=6.-[0,2] where we choose nums1[0], nums1[1], and nums2[2]. The sum of the numbers chosen from nums1 equals the sum of the numbers chosen from nums2:1+2=3.Note that the second and third balanced ranges are different.In the second balanced range, we choose nums2[1] and in the third balanced range, we choose nums1[1].
Example 2:
1
2
3
4
5
6
7
8
9
10
11
Input: nums1 =[0,1], nums2 =[1,0]Output: 4Explanation: The balanced ranges are:-[0,0] where we choose nums1[0]. The sum of the numbers chosen from nums1 equals the sum of the numbers chosen from nums2:0=0.-[1,1] where we choose nums2[1]. The sum of the numbers chosen from nums1 equals the sum of the numbers chosen from nums2:0=0.-[0,1] where we choose nums1[0] and nums2[1]. The sum of the numbers chosen from nums1 equals the sum of the numbers chosen from nums2:0=0.-[0,1] where we choose nums2[0] and nums1[1]. The sum of the numbers chosen from nums1 equals the sum of the numbers chosen from nums2:1=1.
The key idea is to use dynamic programming to count, for every subarray [l, r], the number of ways to pick elements from nums1 or nums2 such that the sum of picked elements from nums1 equals the sum from nums2. We use prefix sums and a DP table to efficiently compute the number of balanced ranges.
Use a DP table where dp[i][d] is the number of ways to pick from the first i elements so that the difference between the sum of picked nums1 and nums2 is d.
For each position, either pick from nums1 or nums2 and update the difference accordingly.
For each subarray, count the number of ways where the difference is zero (i.e., sums are equal).
classSolution {
funcountSubranges(a: IntArray, b: IntArray): Int {
val n = a.size
val mod = 1_000_000_007
var ans = 0for (l in0 until n) {
var dp = mutableMapOf(0 to 1)
for (r in l until n) {
val ndp = mutableMapOf<Int, Int>()
for ((d, cnt) in dp) {
ndp[d + a[r] - b[r]] = (ndp.getOrDefault(d + a[r] - b[r], 0) + cnt) % mod
ndp[d + b[r] - a[r]] = (ndp.getOrDefault(d + b[r] - a[r], 0) + cnt) % mod
}
dp = ndp
ans = (ans + dp.getOrDefault(0, 0)) % mod
}
}
return ans
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
classSolution:
defcountSubranges(self, a: list[int], b: list[int]) -> int:
n, mod, ans = len(a), 10**9+7, 0for l in range(n):
dp = {0: 1}
for r in range(l, n):
ndp = {}
for d, cnt in dp.items():
ndp[d + a[r] - b[r]] = (ndp.get(d + a[r] - b[r], 0) + cnt) % mod
ndp[d + b[r] - a[r]] = (ndp.get(d + b[r] - a[r], 0) + cnt) % mod
dp = ndp
ans = (ans + dp.get(0, 0)) % mod
return ans