Each number extends a snapshot of previously reachable sums; reaching half the total proves an equal partition.
Add the new item to the old snapshot, not to sums just created
Read it this way:Starting from old sums (0,1), item 5 creates only (5,6). Ten is absent because a newly created five cannot reuse that same item. Why is ten absent when this item five can be used only once?
Sources and provenance
Full interview answer 14 min read
Determine whether all input occurrences can be divided into two groups with equal total value.
Problem
Input. A list nums of nonnegative integers, possibly containing zeros and duplicates.
Output. Return True if the occurrences can be partitioned into two equal-sum groups; otherwise return False.
Rules. Every occurrence belongs to exactly one group and may not be reused.
Groups need not be contiguous, and repeated values remain separate occurrences.
Empty groups are allowed, so empty input returns True. Preserve the list; return feasibility rather than group contents.
Example.[1, 5, 11, 5] returns True, using [11] and [1, 5, 5].
For [1, 2, 3, 5], return False.
Picture
After processing 1, the old reachable set is {0,1}.
The first 5 extends only that snapshot:
Reference drawing
Add the new item to the old snapshot, not to sums just created
Derivation
New sums use only the old snapshot. Union happens afterward. Feeding new 5 back into this same update would incorrectly reuse the item.
Read it this way:Starting from old sums (0,1), item 5 creates only (5,6). Ten is absent because a newly created five cannot reuse that same item. Why is ten absent when this item five can be used only once?
The temporary set is complete before union changes possible.
New sums cannot feed more additions in this same occurrence’s update.
After each prefix, possible contains exactly its reachable sums at or below the target.
With nonnegative inputs, an above-target sum cannot later decrease.
That fact permits pruning; signed inputs do not preserve it.
Problem trace
Use the previous reachable set so each item contributes at most once
Input and goalCheck whether the values can be split into two groups with equal sums.
Check the total and seed zeroThe total is 22, so target is 11. Seed {0} using the empty subset.
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
key 0
reachable
next
0
arithmetic
total=1+5+11+5=22; target=22/2=11
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
key 1
not shown→reachable
old set snapshot
not shown→from old {0}: 0+1=1
ignored above target
not shown→none
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
key 5
not shown→reachable
key 6
not shown→reachable
current
0→1
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
key 11
not shown→reachable
current
1→2
old set snapshot
from old {0,1}: 0+5=5; 1+5=6→from old {0,1,5,6}: 0+11=11
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
key 10
not shown→reachable
current
2→3
old set snapshot
from old {0,1,5,6}: 0+11=11→0+5=5; 1+5=6; 5+5=10; 6+5=11
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
subset sum 11
3→2
membership
not shown→11 in possible -> true
partition
not shown→[11] | [1,5,5]
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.
The total is 22, so target is 11. Seed {0} using the empty subset. Step through the frames to watch the state change. The last frame shows the answer or the stopping condition.
Check: Why can the second 5 create ten, but the first 5 cannot reuse itself?
Reference drawing
Problem trace
Use the previous reachable set so each item contributes at most once
Execution trace
Input and goalCheck whether the values can be split into two groups with equal sums.
Check the total and seed zeroThe total is 22, so target is 11. Seed {0} using the empty subset.
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
key 0
reachable
next
0
arithmetic
total=1+5+11+5=22; target=22/2=11
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
key 1
not shown→reachable
old set snapshot
not shown→from old {0}: 0+1=1
ignored above target
not shown→none
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
key 5
not shown→reachable
key 6
not shown→reachable
current
0→1
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
key 11
not shown→reachable
current
1→2
old set snapshot
from old {0,1}: 0+5=5; 1+5=6→from old {0,1,5,6}: 0+11=11
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
key 10
not shown→reachable
current
2→3
old set snapshot
from old {0,1,5,6}: 0+11=11→0+5=5; 1+5=6; 5+5=10; 6+5=11
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
subset sum 11
3→2
membership
not shown→11 in possible -> true
partition
not shown→[11] | [1,5,5]
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.
The total is 22, so target is 11. Seed {0} using the empty subset. Step through the frames to watch the state change. The last frame shows the answer or the stopping condition.
Check: Why can the second 5 create ten, but the first 5 cannot reuse itself?
Read it this way:From reachable sum 0, processing one item of value 2 may add sum 2 only. Forward iteration could incorrectly reuse it to make 4. Which illegal same-item feedback would create four while processing just one two?
The full trace is {0}, {0,1}, {0,1,5,6}, {0,1,5,6,11}, then {0,1,5,6,10,11}.
The two positions holding 5 are distinct occurrences.
Code
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
An odd total cannot split into equal integer sums.
Empty input returns True under the stated empty-group convention.
The Boolean state does not retain a partition witness.
For target T, expected hashing gives O(n(T+1)) state work and O(T+1) space.
This is pseudo-polynomial in the numeric target, not polynomial in its bit length.
Integer arithmetic and hashing costs also depend on representation size.
Change to Dense Coordinates
New task: Use a Boolean array when the target range is compact.
Picture and change: Visit subtotals downward.
Earlier writes then cannot supply the current occurrence again, matching snapshot eligibility.
Inputs must remain nonnegative integers.
def partition_dense(nums): total = sum(nums) if total % 2: return False target = total // 2 possible = [False] * (target + 1) possible[0] = True for value in nums: for subtotal in range(target, value - 1, -1): possible[subtotal] |= possible[subtotal - value] return possible[target]
Check:[2,3,7] returns False; no subset sums to target 6.
An upward update can incorrectly reuse 3 to create 6.
Both implementations accept [1,5,11,5].
Use when: Direct target coordinates cost less than a hash set.
Change to Signed Inputs
New task: Allow arbitrary integer signs.
Picture and change: Keep snapshots but remove the target cap.
An earlier above-target sum may become useful after a negative occurrence.
def partition_signed(nums): total = sum(nums) if total % 2: return False possible = {0} for value in nums: possible |= {subtotal + value for subtotal in possible} return total // 2 in possible
Check:[2,2,-1,-1] returns True: each group can contain 2,-1.
The capped reference discards both early 2 sums and incorrectly returns False outside its input contract.
The unpruned set can have exponentially many distinct sums.
Its cost follows those reachable states, not the old nonnegative target bound.
Use when: Negative values invalidate monotone pruning.
Transfer Check
Why can upward iteration suit unlimited coin reuse but not once-per-occurrence selection?
Answer
Upward writes can become sources later in the same update.
That permits repeated use.
Snapshots or descending updates prevent that feedback.
Mutating a set during its own iteration is not a substitute for either method.