Skip to content
AI360Xpert
Beta
Difficulty: HardBacktracking

Closest Subsequence Sum

Problem in Plain English

Find the smallest absolute difference between a subsequence sum and a target. Split up to forty values, enumerate half sums and search sorted complements.

Problem Statement

Given nums and goal, choose any subsequence, including the empty subsequence, and return the minimum abs(sum-goal). The official exercise allows negative numbers, so a sum that currently exceeds goal cannot safely be pruned. Only the distance is required; different subsequences with the same sum are equivalent answers.

Constraints

  • 1 <= nums.length <= 40.
  • -10^7 <= nums[i] <= 10^7.
  • -10^9 <= goal <= 10^9.
  • The empty subsequence is allowed.

Examples

Example 1

Input
nums = [5,-7,3,5], goal = 6
Output
0

Taking all four values yields 6. Its left sum -2 and right sum 8 give zero distance.

Example 2

Input
nums = [7,-9,15,-2], goal = -5
Output
1

The subset [7,-9,-2] sums to -4, distance one. Enumerating all half pairs shows that no sum equals -5.

Example 3

Input
nums = [1,2,3], goal = -7
Output
7

Every nonempty sum is positive. The empty subset sums to zero and is the closest.

Example 4

Input
nums = [0,0], goal = 1
Output
1

All four subsets sum to zero. Duplicate sums do not change the minimum distance.

Intuition

Forty elements have over a trillion subsets, but twenty have about a million. Divide the array into two halves. Every full subset consists of one subset from each half, so enumerate each half separately and look for pairs whose sums approach goal. In Example 1 the halves [5,-7] and [3,5] produce sums [0,5,-7,-2] and [0,3,5,8]. Left sum -2 pairs with right sum 8 to reach goal 6 exactly. Including zero in both lists represents the empty subset and subsets confined to one half.

Approaches

Meet in the Middle

Optimal

Solution Details

Reveal Meet in the Middle: intuition, complexity, and code

Hints

Hint 1
A full 2^40 search is too large, but two 2^20 lists are feasible.
Hint 2
For a left subset sum x, seek the right subset sum nearest goal-x.
Hint 3
Sort one list, use lower bound, check both adjacent candidates and retain zero for the empty subset.

Edge Cases

  • With one element, one half is empty but still has sum list [0].
  • The target can lie before the first or after the last right sum; check indices before reading.
  • Zeros and repeated values create duplicate sums and remain valid.
  • All positive values with a negative target can make the empty subset optimal.

Common Mistakes and Interview Tips

  • Appending while looping over the growing list uses the same element repeatedly and can run forever.
  • JavaScript default sort orders numbers as text; supply a numeric comparator.
  • Checking only the lower-bound successor misses a closer predecessor.
  • Pruning sums above goal is invalid when later negative values can reduce them.

Key Takeaway

Meet in the Middle trades two manageable exponential lists for one infeasible full search. Enumeration preserves all possibilities, while sorted nearest-neighbor search avoids testing their Cartesian product.