Skip to content
mlmentorship

Extend a snapshot of reachable sums to use each occurrence once

Separate old sums from new additions, then change to dense coordinates or signed inputs without silently permitting reuse.

Published · 14 min read ·Core ·Intermediate

ML breadth · active recall

Practice before you read

8 minutes. Explain the mechanism, why it works, when it fails, and one alternative.

ML breadth · closed-book attempt

Extend a snapshot of reachable sums to use each occurrence once

Explain the mechanism, why it works, when it fails, and one alternative.

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.

08:00recommended time

Closing or reloading clears the scratchpad. Only score, weak rubric dimensions, attempt count, and retry date can be stored locally.

30-second answer map

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

The new item can extend the old sums only onceStarting from old sums (0,1), item 5 creates only (5,6). Ten is absent because a newly created five cannot reuse that same item.Extend only the frozen old snapshotwith the current item0516Add item 5 once to each old valuePublish union 0, 1, 5, 6 onlyafterward.New sums cannot feed back throughthis same 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?
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

The new item can extend the old sums only onceStarting from old sums (0,1), item 5 creates only (5,6). Ten is absent because a newly created five cannot reuse that same item.Extend only the frozen old snapshotwith the current item0516Add item 5 once to each old valuePublish union 0, 1, 5, 6 onlyafterward.New sums cannot feed back throughthis same item.
Derivation
Add the new item to the old snapshot, not to sums just createdTwo source boxes hold only the old reachable sums zero and one. Separate plus-five arrows create five and six. Only afterward, four union boxes retain zero, one, five, six. A separate crossed-out feedback arrow would add the same current item five to the newly created five and incorrectly create ten. No such feedback enters the actual update.ONE NEW ITEM 5; READ ONLY THE OLD SNAPSHOTOLDNEW SUM0+ 5 once51+ 5 once6ONLY NOW UNION OLD AND NEW0156FORBIDDEN: FEED A NEW SUM BACK INTO THIS ITEMnew 5+ 5 again1010 is absent: the current item cannot useitself twice.
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

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

Worked example: descending sums prevent one item from being reused in its own updateFrom reachable sum 0, processing one item of value 2 may add sum 2 only. Forward iteration could incorrectly reuse it to make 4.Extend only the frozen old snapshotwith the current item02Add item 2 once to each old valuePublish union 0, 2 only afterward.New sums cannot feed back throughthis same item.
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

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 , expected hashing gives state work and 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.