Partition Equal Subset Sum solution explained
Partition Equal Subset Sum asks whether a list of positive integers can be split into two groups with the same total. If the total is odd the answer is no; otherwise the question becomes whether some subset adds up to exactly half. A boolean table of reachable sums, filled from high to low for each number, answers that in O(n · S) time, where S is half the total.
By Evin JohnPublished 7 min read
The problem, restated
Two friends are packing parcels into two cars and want both cars to carry the same weight. Each parcel has a whole-number weight and must go in exactly one car. Given the list of weights, decide whether a perfectly even split exists. Nothing asks you to report the split, only whether one exists.
| Weights | Total | Answer | Why |
|---|---|---|---|
| [3, 1, 4, 2, 2] | 12 | true | 4 + 2 = 6 and 3 + 1 + 2 = 6 |
| [7, 3, 3, 1] | 14 | true | 7 on one side, 3 + 3 + 1 on the other |
| [5, 2, 2] | 9 | false | An odd total can never split evenly |
| [10, 1, 2, 3] | 16 | false | 10 is larger than half, so no split works |
| [6] | 6 | false | One parcel cannot be in both cars |
What the interviewer is checking
The key moment is the reduction. Candidates who start building two groups at once get lost quickly. Candidates who say "if one side is half, the other side is automatically half too" have turned a partitioning problem into a single-target subset problem, and from there the 0/1 knapsack pattern applies. The second checkpoint is the loop direction in the one-dimensional table, which shows whether you understand why each number may be used only once.
Likely follow-ups
- Return the two groups. Keep the full two-dimensional table, or remember which number first made each sum reachable, then walk back from half.
- Split into k equal groups. That needs backtracking with pruning or a bitmask DP; the single subset-sum table no longer suffices.
- Minimize the difference instead. Fill the same reachable table up to half and take the largest reachable sum; the difference is total minus twice that sum.
- Why is this fast if subset sum is hard in general? The running time depends on the value of the total, not just on n. It is pseudo-polynomial, so huge weights make it slow.
Brute force
Every number either joins the first group or does not, so there are 2^n possible first groups. Check whether any of them sums to half. With memoization on the pair (index, remaining target), the same recursion becomes O(n · S), which is the table below written top-down.
def can_partition_slow(nums):
total = sum(nums)
if total % 2:
return False
def pick(i, remaining):
if remaining == 0:
return True
if i == len(nums) or remaining < 0:
return False
return pick(i + 1, remaining - nums[i]) or pick(i + 1, remaining)
return pick(0, total // 2)The optimal approach: reachable sums
Keep one boolean per possible sum from 0 to half. At the start only 0 is reachable, by choosing nothing. When a new number x arrives, every sum s that was reachable before also makes s + x reachable. Process the sums from high to low so that the update for s reads reachable[s - x] from before x was considered. If you go upward instead, reachable[s - x] may already include x, and the same number gets counted twice.
def can_partition(nums):
total = sum(nums)
if total % 2:
return False
half = total // 2
reachable = [False] * (half + 1)
reachable[0] = True # the empty group sums to 0
for x in nums:
for s in range(half, x - 1, -1): # downward: each number used once
if reachable[s - x]:
reachable[s] = True
if reachable[half]:
return True
return reachable[half]function canPartition(nums) {
let total = 0;
for (const x of nums) total += x;
if (total % 2 !== 0) return false;
const half = total / 2;
const reachable = new Uint8Array(half + 1);
reachable[0] = 1;
for (const x of nums) {
for (let s = half; s >= x; s--) {
if (reachable[s - x]) reachable[s] = 1;
}
}
return reachable[half] === 1;
}Trace [7, 3, 3, 1] with half 7. After 7, sums {0, 7} are reachable and the early exit fires. Without the exit the table would continue: after the first 3 it holds {0, 3, 7}, after the second 3 it adds 6, and after 1 it adds 1 and 4. The point is that half was reachable, so the answer is true.
The bitset version
A row of booleans is just a row of bits. Store it as one integer where bit s means sum s is reachable. Adding a number x is then a single operation: shift the whole integer left by x and OR it into itself. Python integers grow as needed, so this is short and fast in practice. In C++ the same trick uses a fixed-size std::bitset sized to the largest possible half.
def can_partition_bits(nums):
total = sum(nums)
if total % 2:
return False
reach = 1 # bit s is set when some subset sums to s
for x in nums:
reach |= reach << x
return bool((reach >> (total // 2)) & 1)Complexity
| Approach | Time | Extra memory |
|---|---|---|
| Try every subset | O(2^n) | O(n) recursion |
| Memoized recursion on (index, remaining) | O(n · S) | O(n · S) |
| Two-dimensional table | O(n · S) | O(n · S) |
| One-dimensional table, downward loop | O(n · S) | O(S) |
| Bitset shift and OR | O(n · S / word size) | O(S) bits |
Cases worth testing
- An odd total, which should return false before any table is built.
- A single number, which can never be split.
- One number larger than half, which rules out a split at once.
- Two equal numbers, the smallest true case.
- Many small numbers that reach half only through a long combination.
Mistakes that come up
- Looping the sums upward. On [1, 3], the upward loop marks 2 as reachable by using the 1 twice and wrongly returns true.
- Forgetting reachable[0] = true, so no sum ever becomes reachable.
- Sizing the table by the full total instead of half, which wastes memory and time.
- Trying a greedy rule such as sorting and filling the lighter side, which fails on lists like [3, 3, 2, 2, 2].
- Calling the solution polynomial without noting that it scales with the size of the numbers.
How to explain it out loud
Fictional example
- Candidate
- If the total is odd I can stop. Otherwise I only need one group that sums to half, because the leftovers make the other half.
- Candidate
- That is subset sum. I keep a boolean for every sum up to half. Each number turns every reachable s into a reachable s plus that number, and I walk the sums from the top down so a number cannot be used twice in one pass.
- Candidate
- It is n times half the total in time and half the total in memory. If the weights were in the millions I would say so, because the cost grows with the values, not just the count.
Related problems
Target Sum reduces to the same subset question but counts how many subsets hit the target instead of asking whether one exists. Coin Change II flips the loop direction, because there each coin may be reused. Solving all three in a row makes the loop-direction rule stick.
Practicing with Natively
The reduction to half the total is the step most people need to rehearse aloud. While you practice, Natively on Mac or Windows can read the problem from a screenshot and offer an approach to compare against your own, and it keeps a transcript of your mock so you can check that you justified the downward loop. See the AI interview assistant for how practice sessions work.
Questions people ask
How do you solve Partition Equal Subset Sum?
Return false if the total is odd. Otherwise check whether some subset sums to half the total, using a boolean array of reachable sums that each number updates from the highest sum down to the number itself.
Why does the inner loop go backward?
Going from high sums to low ones means each update reads values from before the current number was added, so the number is used at most once. An upward loop can reuse it and give wrong answers.
What is the time complexity of Partition Equal Subset Sum?
O(n · S) time and O(S) memory, where S is half the total. It is pseudo-polynomial, because it grows with the size of the numbers as well as their count.
Is Partition Equal Subset Sum a knapsack problem?
Yes. It is the 0/1 knapsack pattern in its yes-or-no form: each item is taken once or not at all, and the question is whether a capacity of exactly half can be filled.
Where to go next
Practice with the same assistant you would use on a call: it listens, transcribes and suggests answers from your own resume and the job description.
Facts on this page were last checked on . Product details come from the Natively app itself; if something here is out of date, tell us at hello@heynatively.com.
