0% completed
You are given an integer array nums
containing n
integers, integer A
and integer B
.
Return the number of subsets of the nums
have a sum that falls between the values A
and B
(inclusive).
Example 1:
Example 2:
Example 3:
To solve the problem of counting subsets whose sums fall within a specific range, we use the Meet in the middle approach. The idea is to divide the original array into two halves and then generate all possible subset sums for each half. By working with smaller subsets, we reduce the problem's complexity, making it more manageable. The sums of subsets from the first half are stored in one list, while the sums from the second half are stored in another. The second list is then sorted to enable efficient binary search operations.
After generating and sorting these subset sums, we determine how many pairs of sums from the two lists fall within the given range ([A, B]). For each sum from the first list, we calculate the required range in the second list using simple arithmetic adjustments. Binary search is then employed to quickly count how many sums in the second list fall within this adjusted range. The total count of such valid subsets is returned as the final result. This method significantly reduces the time complexity compared to a brute-force approach, making it effective for handling larger arrays.
Calculate the Length and Midpoint of the Array:
nums
and store it in the variable n
.n / 2
and store it in the variable mid
. This will allow us to split the array into two halves.Split the Array into Two Halves:
Arrays.copyOfRange
method to create two new arrays: set1
and set2
.set1
contains elements from the start of nums
to the midpoint, i.e., nums[0]
to nums[mid-1]
.set2
contains elements from the midpoint to the end of nums
, i.e., nums[mid]
to nums[n-1]
.Generate All Possible Subset Sums for set1
:
generateSubsetSums
method with set1
as the input.sum1
containing all possible subset sums of set1
.Generate All Possible Subset Sums for set2
:
generateSubsetSums
method with set2
as the input.set1
.sum2
containing all possible subset sums of set2
.Sort the List of Subset Sums for set2
:
Collections.sort
to sort the list sum2
. Sorting this list allows us to efficiently find how many subset sums fall within a specific range using binary search.Initialize a Count Variable:
count
to 0
. This will be used to keep track of the total number of valid subsets whose sum lies between A
and B
.Iterate Over Each Sum in sum1
:
s1
in sum1
.s1
, calculate the low
and high
bounds:
low = A - s1
: This is the lower bound of the desired sum range when combined with a sum from sum2
.high = B - s1
: This is the upper bound of the desired sum range when combined with a sum from sum2
.sum2
.Count Valid Sums in sum2
:
countInRange
method with sum2
, low
, and high
as arguments.countInRange
method:
lowerBound
to find the first index in sum2
where the elements are not less than low
.upperBound
to find the first index in sum2
where the elements are greater than high
.right - left
gives the count of valid sums in sum2
that fall within the range [low, high]
.count
.Return the Total Count:
sum1
, return the final value of count
. This value represents the total number of subsets whose sum is within the range [A, B]
.Input Preparation:
nums = [1, -1, 2, 3]
and the range [A, B] = [1, 4]
.mid = 4 / 2 = 2
.Split the Array:
nums
into two halves:
set1 = [1, -1]
set2 = [2, 3]
Generate Subset Sums for set1
:
set1 = [1, -1]
:
[]
→ Sum 0
[1]
→ Sum 1
[-1]
→ Sum -1
[1, -1]
→ Sum 0
sum1 = [0, 1, -1, 0]
Generate Subset Sums for set2
:
set2 = [2, 3]
:
[]
→ Sum 0
[2]
→ Sum 2
[3]
→ Sum 3
[2, 3]
→ Sum 5
sum2 = [0, 2, 3, 5]
Sort sum2
:
sum2
, though it is already sorted in this case: sum2 = [0, 2, 3, 5]
.Count Valid Subsets:
Initialize count = 0
.
Iterate over each sum s1
in sum1
and calculate the range [low, high]
for valid sums in sum2
:
s1 = 0
:
low = 1 - 0 = 1
high = 4 - 0 = 4
sum2
, valid sums are 2
and 3
.count = count + 2 = 2
.s1 = 1
:
low = 1 - 1 = 0
high = 4 - 1 = 3
sum2
, valid sums are 0
, 2
, and 3
.count = count + 3 = 5
.s1 = -1
:
low = 1 - (-1) = 2
high = 4 - (-1) = 5
sum2
, valid sums are 2
, 3
, and 5
.count = count + 3 = 8
.s1 = 0
:
low = 1 - 0 = 1
high = 4 - 0 = 4
sum2
, valid sums are 2
and 3
.count = count + 2 = 10
.Final Output:
sum1
, the final count of valid subsets is 10
.10
as the output.Set1
and Set2
), the code generates all possible subset sums. Since there are N/2
elements in each half, the number of subsets is 2<sup>(N/2)</sup>. This gives us O(2<sup>(N/2)</sup>) operations for each half.Sum2
):
Sum2
list takes O(2<sup>(N/2)</sup> * log(2<sup>(N/2)</sup>)), which simplifies to O(2<sup>(N/2)</sup> * N/2) or O(N * 2<sup>(N/2)</sup>).Sum1
, we perform a binary search on Sum2
to count the number of valid pairs. This step takes O(2<sup>(N/2)</sup> * log(2<sup>(N/2)</sup>))or
O(2<sup>(N/2)</sup> * N/2).Sum1
and Sum2
):
.....
.....
.....