Problem Description
Given an integer array nums and an integer goal, we want to choose a subsequence (i.e., any subset of elements, including possibly none or all) such that the absolute difference between the sum of the subsequence and goal is minimized. Return this minimum possible absolute difference.
Key Insights
- The array length can be up to 40, making the total number of subsequences (2^n) too large for direct enumeration.
- Use meet-in-the-middle: split the array into two halves and generate all possible sums for each half.
- Sort one half’s sums (e.g., the right half) to enable efficient binary search.
- For each sum in the left half, use binary search in the right half to find a complementary sum that minimizes the difference with goal.
- The empty subsequence (sum = 0) is naturally included in the generated sums.
Space and Time Complexity
Time Complexity: O(2^(n/2) * log(2^(n/2)))
Space Complexity: O(2^(n/2)) for storing the subset sums from each half
Solution
We split the given nums array into two halves. For each half, we generate all possible subset sums — this is feasible because each half has at most 20 elements (2^20 possible sums). One of the lists (rightSums) is sorted to allow binary search. For each sum in leftSums, we compute the remainder (goal - leftSum) and perform binary search in rightSums to find the candidate sums closest to this remainder. We update the result with the minimum absolute difference found between (leftSum + rightCandidate) and goal. This approach effectively reduces the exponential factor by splitting the problem and leveraging efficient search.