Check whether the values can be split into two groups with equal sums.
Start with the concrete trace below. It shows the state the algorithm must carry as it runs.
Problem trace
Partition Equal Subset Sum: Each number extends a snapshot of previously reachable sums; reaching half the total proves an equal partition.
Input and goalCheck whether the values can be split into two groups with equal sums.
Check the total and seed zeroFor nums = [1, 5, 11, 5], total = 22 is even, so target = 11. The empty subset makes only sum 0 reachable.
next=1105111253
mapLabelpossible sums at or below target 11arithmetictotal=1+5+11+5=22; target=22/2=11branch22 % 2 = 0, continue
possible sums at or below target 110reachable
Process 1The set-comprehension snapshot is {0}. It adds 0 + 1 = 1, then union keeps both 0 and 1.
current=1105111253
mapLabelpossible sums at or below target 11oldSetSnapshotfrom old {0}: 0+1=1ignoredAboveTargetnone
possible sums at or below target 110reachable1reachable
Process the first 5From old {0,1}, add 0 + 5 = 5 and 1 + 5 = 6. The reachable set becomes {0,1,5,6}.
10current=55111253
mapLabelpossible sums at or below target 11oldSetSnapshotfrom old {0,1}: 0+5=5; 1+5=6ignoredAboveTargetnone
possible sums at or below target 110reachable1reachable5reachable6reachable
Process 11Only 0 + 11 stays within target. Sums 1, 5, and 6 would exceed 11, so union adds 11.
1051current=1111253
mapLabelpossible sums at or below target 11oldSetSnapshotfrom old {0,1,5,6}: 0+11=11ignoredAboveTarget12, 16, 17
possible sums at or below target 110reachable1reachable5reachable6reachable11reachable
Process the final 5The old-set snapshot adds 5, 6, 10, and 11. Values 16 are ignored; union preserves existing sums and adds 10.
1051112current=553
mapLabelpossible sums at or below target 11oldSetSnapshot0+5=5; 1+5=6; 5+5=10; 6+5=11ignoredAboveTarget11+5=16
possible sums at or below target 110reachable1reachable5reachable6reachable10reachable11reachable
Test target membershipAfter every input value is processed, target 11 is in possible. Subset [11] has sum 11 and the remaining [1,5,5] also sums to 11.
1051subset sum 1111253
mapLabelfinal possible sumsmembership11 in possible -> truepartition[11] | [1,5,5]arithmetic11 = 11resulttrue
final possible sums0reachable1reachable5reachable6reachable10reachable11reachable
Recognize it
The input must split into equal-sum groups, so one group must realize exactly half the total; each positive number is either included once or excluded.
Keep true
After processing a prefix of nums, possible contains exactly the sums at most target realizable from that prefix. New sums are computed from an old-set snapshot, so the current number cannot be reused within its own iteration.
Reuse it
Convert a partition condition into a target-reachability problem, then update from a snapshot when each item may be used once. The same state model solves 0/1 knapsack feasibility and constrained subset sums.
Read it this way: For nums = [1, 5, 11, 5], total = 22 is even, so target = 11. The empty subset makes only sum 0 reachable. Step through the frames to watch the state change. The last frame shows the answer or the stopping condition.
Pattern: Subset-sum DP.
Simple idea: The wanted sum is half the total. Keep every sum that can be made from the
values processed so far.
def can_partition(nums: list[int]) -> bool: total = sum(nums) if total % 2: return False target = total // 2 possible = {0} for num in nums: possible |= {value + num for value in possible if value + num <= target} return target in possible