Subset Sum Problem
Pattern: Subsequences with sum
Keep a table can[s] meaning "some subset of the values seen so far sums to s". Each new value v sets can[s] for every s that was reachable at s - v. Walking s downwards uses each value at most once.
No verified practice link yet. The guide below states the problem in full, so you can still solve it here.
The problem
Given an array of positive whole numbers and a target sum, return whether some subset adds up to exactly sum. For
example, [3, 34, 4, 12, 5, 2] with sum = 9 gives true, and with sum = 30 gives false.
This problem is from Love Babbar's 450 sheet, and this guide has no verified link for it yet. The function name
isSubsetSum is this guide's own. This page solves it with a table instead of plain recursion, because the target is
small.
Think first
- Which sums can the empty subset make?
- If you know every sum reachable with the first
ivalues, how do you add valuei + 1? - Why might going upwards through
suse a value twice?
Hints
Brute force, and why it fails
Take-or-skip recursion is O(2ⁿ). The table is O(n × sum), which is much faster when sum is small.
The key insight
Many different subsets give the same sum. Remembering only which sums are reachable merges them all, turning an
exponential search into a table of sum + 1 booleans.
Dry run
| reachable | 0 |
|---|
arr = [3, 34, 4, 12, 5, 2] and sum = 9. At first only 0 is reachable.
The plan
can = array of sum + 1 falses; can[0] = true
for v in arr:
for s from sum down to v:
if can[s - v]: can[s] = true
return can[sum]
Complexity
- Time
O(n × sum)- Space
O(sum)
Each value updates at most sum entries, and one row of sum + 1 booleans is kept.
Edge cases
sum = 0: true (the empty subset).- A single value equal to
sum: true. - Values larger than
sumchange nothing.
I've built my logic, show the code
Java
class Solution {
public boolean isSubsetSum(int[] arr, int sum) {
boolean[] can = new boolean[sum + 1]; // can[s]: some subset of the values seen so far sums to s
can[0] = true; // the empty subset
for (int v : arr) {
for (int s = sum; s >= v; s--) { // go down, so each value is used at most once
if (can[s - v]) can[s] = true;
}
}
return can[sum];
}
}C++
class Solution {
public:
bool isSubsetSum(vector<int>& arr, int sum) {
vector<bool> can(sum + 1, false); // can[s]: some subset of the values seen so far sums to s
can[0] = true; // the empty subset
for (int v : arr) {
for (int s = sum; s >= v; s--) { // go down, so each value is used at most once
if (can[s - v]) can[s] = true;
}
}
return can[sum];
}
};Python
class Solution:
def isSubsetSum(self, arr: list[int], total: int) -> bool:
can = [False] * (total + 1) # can[s]: some subset of the values seen so far sums to s
can[0] = True # the empty subset
for v in arr:
for s in range(total, v - 1, -1): # go down, so each value is used at most once
if can[s - v]:
can[s] = True
return can[total]Common mistakes
- Looping
supwards, which lets a value be used many times. - Forgetting
can[0] = true. - Using this table when
sumis huge, where the memory explodes.