Subset Sum Equals Target
Subset Sum Equals Target is a GFG problem (Medium). You get an array arr of positive integers and a number sum. Return true if some of the elements, each used at most once, add up to exactly sum, and false otherwise.
- You only answer yes or no. The elements themselves are not returned.
sumis at most 10,000, so a table with one cell per possible total is small. Trying every subset of 200 elements is not an option.
- 1 <= arr.length <= 200
- 1 <= arr[i] <= 200
- 1 <= sum <= 10^4
Intuition
Track which totals can be reached instead of listing subsets. Keep a yes/no array can with one cell for every total from 0 to sum. At the start only 0 is reachable: picking nothing gives 0.
When an element x arrives, every total you could already reach can now be pushed up by x. So cell t turns on when cell t − x was already on. After the last element, can[sum] is the answer.
- Walk from high totals to low: the cell you read,
t − x, has then not been touched forxyet, soxis counted at most once. - Why it is fast: each element costs at most
sum + 1checks, however many subsets exist.
This is the classic subset sum problem: each item is either taken once or left out, and the question is whether a total can be hit exactly. That is the 0/1 knapsack shape with only yes/no values, and the total is small enough to index an array.
Approach
Before reading on, decide what one cell of the table should mean, and what a single new element can change in it. Then check your update with arr = [3] and sum = 6: the answer must be false. Aim for O(n · sum) time and O(sum) space.
Two ways to solve it
Keep one yes/no cell per total and update it element by element, walking from high totals to low.
- No recursion: nothing to overflow.
- Early exit: stops when
sumturns on.
The usual final answer.
Ask whether elements from index i onward can make left, by either taking arr[i] or skipping it, and cache each (i, left) answer.
- Closest to the definition: the easiest first version to write.
- Cost: a full 2D cache plus a recursion stack up to n deep.
Both check each pair of (element, total) at most once. The 1D array wins on memory and has no recursion. The steps, code and live demo follow the array, and the memoised recursion comes after the demo.
Start with only zero reachable
Create can of length sum + 1, all false, and set can[0] = true. The empty pick reaches 0, and every other total has to be built from it by adding elements one at a time.
Add one element at a time
For each x, walk t from sum down to x. If can[t - x] is true, set can[t] to true, because the pick that reached t - x plus x reaches t. Totals below x cannot include x, so the walk stops there.
Skip elements that are too big
When x is larger than sum, the range from sum down to x is empty and nothing changes. No special case is needed: no total that matters can contain that element.
Stop as soon as the target is on
After each element, return true if can[sum] is on. Later elements only switch more cells on and never switch one off, so the answer cannot change. If the loop ends first, return can[sum], which is then false.
Code in Python, C++ and Java
Take-or-skip recursion with memo
can(i, left) asks whether the elements from index i onward can add up to left. Taking arr[i] is only tried when it fits in left. Each (i, left) pair is computed once and cached, which turns 2^n paths into at most n · sum states.
Common pitfalls
Walking the totals upward
for t in range(x, sum + 1):
if can[t - x]:
can[t] = Truefor t in range(sum, x - 1, -1):
if can[t - x]:
can[t] = TrueWith arr = [3] and sum = 6, the upward walk turns on 3, then reads that fresh 3 and turns on 6, so it returns true. It used the single 3 twice. That loop solves the version where every element can repeat, like coin change.
Forgetting that zero is reachable
can = [False] * (sum + 1)
can = [False] * (sum + 1) can[0] = True
Every reachable total is built by adding to an earlier one, and the chain has to start at the empty pick. Without can[0], nothing ever turns on, so arr = [5] with sum = 5 returns false.
Edge cases
sum is the total of the whole arrayThe last element turns can[sum] on, so [1, 2, 3] with sum = 6 returns true. Taking every element is a valid subset.
sumNo inner loop runs, only can[0] is on, and the function returns false, as for [7, 8] with sum = 5.
Complexity
sum + 1 cells, which is at most 2 million checks here. The textbook subset sum DP uses a 2D table dp[i][t] with the same 2 million cells, but each row only reads the row before it, so one array is enough.Subset Sum Equals Target FAQ
Why is this called pseudo-polynomial time?
The running time grows with the value of sum, not with how many digits it has. With sum up to 10,000 that is cheap, but a target near 10^18 would need an impossible table. In general, subset sum is NP-complete, so no known method is polynomial in the input size.
How do I find which elements make the target?
Keep the full 2D table, where dp[i][t] says whether the first i elements can reach t. Start at (n, sum) and walk back: if dp[i - 1][t] is already true, element i is not needed; otherwise take arr[i - 1] and move to t - arr[i - 1]. Stop when t reaches 0.
How is this different from Subset Sums and Partition Equal Subset Sum?
Subset Sums lists the total of every subset, all 2^n of them, so it has to enumerate. Here only one total matters, so a table of totals replaces the list. Partition Equal Subset Sum is this same check with sum set to half of the array total.