Choose Numbers From Two Arrays in Range - Solution & Explanation
Problem Statement
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 to the sum of the numbers you pick fromnums2(the sum is considered to be0if 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 != 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 109 + 7.
Example 1:
Input: nums1 = [1,2,5], nums2 = [2,6,3] Output: 3 Explanation: 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:
Input: nums1 = [0,1], nums2 = [1,0] Output: 4 Explanation: 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.
Constraints:
n == nums1.length == nums2.length1 <= n <= 1000 <= nums1[i], nums2[i] <= 100
Approach Overview
Problem Overview: You have two arrays of equal length. At every index i, you must choose either nums1[i] or nums2[i]. The goal is to count how many different selections produce a total sum that lies inside a given range [lower, upper].
Approach 1: Brute Force Enumeration (O(2^n) time, O(n) space)
The most direct approach explores every possible choice. For each index, branch into two paths: pick nums1[i] or nums2[i]. Maintain the running sum and check if the final value falls inside [lower, upper]. This generates 2^n combinations, which becomes infeasible even for moderate n. This approach is useful only to reason about the search space and verify correctness on very small inputs.
Approach 2: Dynamic Programming on Sum States (O(n * R) time, O(R) space)
The key observation: the only information that matters at index i is the current cumulative sum. Let dp[s] represent the number of ways to reach sum s after processing some prefix of the arrays. For each position i, transition to the next state by adding either nums1[i] or nums2[i]. This becomes a classic state transition over sums. Because sums can be negative, apply an offset when storing them in an array. The number of tracked sums is bounded by the possible value range, which keeps the state space manageable.
Approach 3: DP with Prefix Sum Optimization (O(n * R) time, O(R) space)
When the valid range of sums becomes large, repeatedly updating individual states can be slow. A more efficient technique uses prefix sums to aggregate transitions over ranges. Instead of updating each target sum independently, accumulate contributions using a prefix array and convert them back to counts in one pass. This reduces constant factors and keeps the DP transitions efficient even when the allowed sum interval is wide. The technique is common in dynamic programming problems that involve range transitions.
The DP array is updated for each index while preserving only the current row, so memory stays proportional to the tracked sum range. The final answer is the total number of ways where the ending sum lies between lower and upper.
Recommended for interviews: The dynamic programming formulation with compressed state is the expected solution. Start by describing the brute force choice tree to show understanding of the search space. Then convert it into a DP over cumulative sums. Interviewers usually look for the insight that each position only depends on the previous set of reachable sums, which turns the exponential search into a polynomial-time solution using arrays and DP state transitions.
Solution
Code
Python
Java
C++
Go
TypeScript
Detailed Complexity Analysis
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Brute Force Enumeration | O(2^n) | O(n) | Understanding the decision tree or validating small inputs |
| Dynamic Programming on Sum States | O(n * R) | O(R) | General case where cumulative sums fall within a manageable range |
| DP with Prefix Sum Optimization | O(n * R) | O(R) | Large transition ranges where batching updates with prefix sums reduces overhead |
Video Solution
Median of Two Sorted Arrays - Binary Search - Leetcode 4 • NeetCode • 551,948 views views
Watch 9 more video solutions →Frequently Asked Questions
Is Choose Numbers From Two Arrays in Range easy or hard?
Choose Numbers From Two Arrays in Range Python/Java solution
How to solve Choose Numbers From Two Arrays in Range in O(n)?
What is the best approach for Choose Numbers From Two Arrays in Range?
Is Choose Numbers From Two Arrays in Range asked at Google/Amazon/Meta?
What data structure is used in Choose Numbers From Two Arrays in Range?
What is the time complexity of Choose Numbers From Two Arrays in Range?
Ready to solve this problem?
Practice Choose Numbers From Two Arrays in Range with our built-in code editor and test cases.
Practice on FleetCode