Partition Equal Subset Sum
Partition Equal Subset Sum is LeetCode 416 (Medium). Given an array nums of positive integers, return true if it can be split into two groups with the same sum, and false otherwise. Every number goes into exactly one of the two groups.
- There are up to 200 numbers, so trying all 2²⁰⁰ ways to split them is impossible.
- Each number is at most 100, so the total is at most 20,000. That small total is what makes a table over sums possible.
- 1 <= nums.length <= 200
- 1 <= nums[i] <= 100
Intuition
If the two groups have equal sums, each one holds exactly half of the total. So the split exists exactly when some subset adds up to total / 2: put that subset in one group, and the rest automatically adds up to the other half. An odd total can never be halved, which settles it at once.
Now keep a row of yes/no answers: dp[s] says whether some of the numbers seen so far add up to s. Each new number num makes s reachable whenever s − num already was. You only need sums up to the half, and you can stop the moment the half itself becomes reachable.
"Split into two groups with a relation between their sums" usually turns into "does one subset reach a fixed sum?" once you notice the second group is determined by the first. Small values with a bounded total point to a table indexed by sum.
Approach
Before reading on, write what the sum of one group must be if the two groups are equal. Then decide what a yes/no table indexed by sum should mean after each number. Aim for O(n · total) time and O(total) space.
Halve the total
Add up nums. If the total is odd, return false. Otherwise set target = total / 2; the question is now whether any subset of nums sums to target.
Start with only the empty sum
Make a boolean row dp of length target + 1, all false, and set dp[0] = true. Before any number is used, the only sum you can make is 0, by choosing nothing.
Add one number, scanning sums downwards
For each num, go from s = target down to num and set dp[s] = dp[s] or dp[s − num]. Downwards, dp[s − num] still describes subsets without this number, so a single 5 can never be used twice to reach 10.
Stop as soon as the half is reachable
After each number, if dp[target] is true, return true; one group is found, and the leftover numbers form the other. If every number is processed and it is still false, return false.
Code in Python, C++ and Java
Common pitfalls
Scanning the sums upwards
for s in range(num, target + 1):
for s in range(target, num - 1, -1):
Upwards, a sum made with num a moment ago is read again, so one number counts several times. For nums = [1, 5] (target 3) the upward scan reaches 1, then 2 from 1 + 1, then 3 from 2 + 1, and wrongly returns true.
Sorting and filling the groups greedily
sort descending, add each number to the smaller group
check whether total / 2 is a reachable sum
For nums = [3, 3, 2, 2, 2] the greedy puts 3 and 3 apart, then 2, 2 and 2 make groups 5 and 7. But 3 + 3 = 6 and 2 + 2 + 2 = 6 is a valid split.
Edge cases
The target is 7 and the scan for 10 never runs, because 10 > 7. Whichever group holds the 10 is already over half, so false is correct, and the code needs no special check.
One positive number cannot be split into two equal groups. The total is either odd, or the target is half the number and that one number is too large to land on it, so the answer is false.
Complexity
Partition Equal Subset Sum FAQ
How does this differ from the 0/1 knapsack?
The 0/1 knapsack maximises value under a weight limit. Here each number is both its weight and its value, and you only ask whether one exact sum is reachable, so each cell holds yes or no instead of a best value.
Can it be made faster with bits?
Yes. Keep the reachable sums as bits of one big integer and do bits |= bits << num for each number; bit target tells the answer. It is the same table, but each shift updates many sums at once, which in C++ is often written with bitset.
What if the numbers could be negative?
Then sums can drop below 0 and the table needs an offset, or a set of reachable sums instead of an array. The halving idea still holds; the problem rules out negatives so the simple row works.