LeetCode #416 Medium

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.
Constraints
  • 1 <= nums.length <= 200
  • 1 <= nums[i] <= 100
dynamic-programmingarrayknapsack
Open on LeetCode ↗
02

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.

How to spot this pattern

"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.

03

Approach

Try it first

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.

1

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.

2

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.

3

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.

4

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.

04

Code in Python, C++ and Java

▶1class Solution:
▶2 def canPartition(self, nums: List[int]) -> bool:
▶3 total = sum(nums)
▶4 if total % 2:
▶5 return False
▶6 target = total // 2
▶7 dp = [False] * (target + 1)
▶8 dp[0] = True
▶9 for num in nums:
▶10 for s in range(target, num - 1, -1):
▶11 dp[s] = dp[s] or dp[s - num]
▶12 if dp[target]:
▶13 return True
▶14 return False
numbers123total 6, half 3sumreach?0yes1no2no3no↑ halfaim for 3; only sum 0 is reachable
total6
target3
If one group adds up to 3, the rest adds up to the other 3. So the question becomes: can some of the numbers reach exactly 3? With nothing chosen yet, only 0 is reachable.
numbers123total 6, half 3sumreach?0yes1no2no3no↑ half+1sum 3: 2 not reachable → no
num1
sum3
reachableno
Earlier numbers cannot make 2, so adding 1 cannot make 3 either. It stays unreachable for now.
numbers123total 6, half 3sumreach?0yes1no2no3no↑ half+1sum 2: 1 not reachable → no
num1
sum2
reachableno
Earlier numbers cannot make 1, so adding 1 cannot make 2 either. It stays unreachable for now.
numbers123total 6, half 3sumreach?0yes1yes2no3no↑ half+1sum 1: 0 was reachable → yes
num1
sum1
reachableyes
Earlier numbers can make 0, and adding 1 to that group makes 1. Because the scan runs downwards, 0 has not been touched by 1 yet, so 1 is used only once.
numbers123total 6, half 3sumreach?0yes1yes2no3yes↑ half+2sum 3: 1 was reachable → yes
num2
sum3
reachableyes
Earlier numbers can make 1, and adding 2 to that group makes 3. Because the scan runs downwards, 1 has not been touched by 2 yet, so 2 is used only once.
numbers123total 6, half 3sumreach?0yes1yes2yes3yes↑ half+2sum 2: 0 was reachable → yes
num2
sum2
reachableyes
Earlier numbers can make 0, and adding 2 to that group makes 2. Because the scan runs downwards, 0 has not been touched by 2 yet, so 2 is used only once.
numbers123total 6, half 3sumreach?0yes1yes2yes3yes↑ halfhalf reached → true
returntrue
Some of the numbers add up to 3. They form one group, and the others add up to 3, so the split exists and the loop stops early.
05

Common pitfalls

Scanning the sums upwards

✗ Wrong
for s in range(num, target + 1):
✓ Right
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

✗ Wrong
sort descending, add each number to the smaller group
✓ Right
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.

06

Edge cases

One number larger than half the total, like [2, 2, 10]

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.

A single number

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.

07

Complexity

Time
O(n · target)
Space
O(target)
Each of the n numbers scans at most target + 1 sums, and target is at most 10,000, so the worst case is about 2 million updates. Only one row of target + 1 booleans is kept.
08

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.