Subset Sums
Subset Sums is a GFG problem (Medium). Given an array arr of integers, return the sums of all subsets of the array, in any order. The empty subset counts, with total 0.
- An array of n elements has 2^n subsets, so the answer holds 2^n numbers. With n at most 15, that is at most 32,768 values.
- Equal elements still form different subsets, so repeated totals stay in the output.
- 1 <= arr.length <= 15
- 0 <= arr[i] <= 10^5
Intuition
Build the list of totals one element at a time. Before any element, the only subset is the empty one, so the list is [0].
When a new element x arrives, every subset so far splits in two: a copy without x, which keeps its total, and a copy with x, whose total grows by x. So the new list is the old list plus every old total increased by x.
The list doubles with each element, and after n elements it holds 2^n totals, one per subset. No subset is ever built or copied. Only totals are kept, so each new total costs a single addition.
The output is one number per subset, so the answer itself is exponential. When every element is independently in or out and only a running total matters, carry the totals instead of the subsets.
Approach
Before reading on, write down the totals for one element, then for two. How does the second list come from the first? Aim for O(2^n) time.
Two ways to solve it
Start from [0] and, for each element, append every old total plus that element.
- No recursion: two plain loops.
- Easy to check: the list length doubles each round.
The shortest correct version.
Walk the array with a running total; at each index, branch once with the element added and once without, and record the total when the index reaches the end.
- Same tree of choices drawn as recursion.
- Watch out: record only at the end, not at every call.
Both do one addition per total. The list version has no recursion and is easier to trace, so the steps, code and live demo follow it. The recursion comes after the demo.
Start from the empty subset
Set sums = [0]. The empty subset is always part of the answer, and every other subset grows out of it by adding elements one at a time.
Double the list for each element
For each x, take the m totals that were in the list before x arrived and append sums[k] + x for each of them. The first half of the list is now every subset without x, and the second half is every subset with it.
Why every subset appears exactly once
After the first i elements, the list has one total per subset of those i elements. A subset of i + 1 elements either skips the new one, so it is in the old half, or takes it, so it is in the new half. It cannot be in both, and it cannot be missing.
Return the list unsorted
Return sums as built. The checker sorts the output before comparing, so spending O(2^n log 2^n) on a sort of your own buys nothing at all.
Code in Python, C++ and Java
Take-or-skip recursion
go(i, s) has decided the elements before index i, and s is the total of the ones it took. It branches into taking arr[i] and skipping it. When i reaches the end, every element is decided, so s is one subset's total and is recorded.
Common pitfalls
Growing the list while looping over it
for s in sums:
sums.append(s + x)m = len(sums)
for k in range(m):
sums.append(sums[k] + x)The loop keeps meeting the totals it just appended, so even arr = [2] never finishes in Python. Java throws ConcurrentModificationException, and a C++ range loop over a vector that reallocates is undefined behaviour. Fix the count before the loop.
Removing repeated totals
return sorted(set(sums))
return sums
For [1, 2, 1] the expected answer has 8 values, [0, 1, 1, 2, 2, 3, 3, 4], because the two 1s are different subsets. A set keeps only 5. Distinct sums are a separate problem.
Edge cases
A zero still doubles the list, so [0, 5] gives [0, 0, 5, 5]. Taking or skipping the zero are two different subsets with the same total.
Complexity
Subset Sums FAQ
How is this different from listing the subsets themselves?
Subsets returns the subsets themselves, and copying each one costs up to n, so it runs in O(n · 2^n). Here only the totals are needed, so each subset costs O(1) and the whole run is O(2^n).
Do I need every sum to check one target?
No. If the question is whether some subset hits one given total, keep a yes/no table of reachable totals instead, as in Subset Sum Equals Target. That costs O(n · target) rather than O(2^n).
What if the array had 40 elements?
2^40 totals is too many, but the same doubling still helps. Split the array into two halves of 20, list each half's totals (about a million each), sort one list, and combine the halves with binary search or two pointers. This is called meet in the middle.