Skip to content

Partition Equal Subset Sum

Decide whether an array can be split into two groups with equal sums, using a table of reachable sums.

Medium1D Dynamic Programming
Solve on LeetCode

The problem

You get an array nums of positive integers. Return true if you can split it into two groups with equal sums, where every number goes into exactly one group.

Input nums = [3, 1, 4, 2, 2]

Output true

The total is 12, and {4, 2} and {3, 1, 2} both add up to 6.

Input nums = [1, 2, 5]

Output false

The total 8 is even, but no group of these numbers adds up to 4.

Key constraint: There are up to 200 numbers of at most 100 each, so half the total is at most 10,000: small enough for a table of reachable sums.

How to spot the pattern

  • Splitting into two equal halves is the same as finding one subset that adds up to half the total.
  • Each number is used at most once, which makes it a 0/1 knapsack.
  • The sums are small, so you can track which totals are reachable.

The idea

If the total is odd, no split exists. Otherwise you only need one subset that adds up to target = total / 2, because the remaining numbers make up the other half. State: can[s] is true when some of the numbers seen so far add up to s; a new number x lets every reachable s - x reach s too, so can[s] = can[s] or can[s - x], updated from high s to low so that each number is used once.

Brute force: Try all 2^n ways to put the numbers into two groups, which takes O(2^n) time.

Step by step

  1. 1Add up the numbers, and return false if the total is odd.
  2. 2Set target = total / 2 and make can of size target + 1 with only can[0] true.
  3. 3For each number x, loop s from target down to x and set can[s] to true when can[s - x] is true.
  4. 4Return can[target].

See the pattern move

The 1d dynamic programming pattern on a small example. Step through it, or press play.

Code

class Solution:
    def canPartition(self, nums: List[int]) -> bool:
        total = sum(nums)
        if total % 2 == 1:
            return False
        target = total // 2
        # can[s] is True when some of the numbers seen so far add up to s
        can = [False] * (target + 1)
        can[0] = True
        for x in nums:
            # go downwards so each number is used at most once
            for s in range(target, x - 1, -1):
                if can[s - x]:
                    can[s] = True
        return can[target]

Complexity

Time

O(n * target)

Space

O(target)

Each of the n numbers sweeps the table of size target + 1 once.

Watch out for

  • Looping s upward: then can[s - x] may already include x from this same round, so one number gets used twice.
  • Skipping the odd-total check, so integer division gives a target that no split can match.

Follow-up questions

Can you make it faster in practice?

Stop as soon as can[target] turns true. In Python, a big integer used as a bitset (bits |= bits << x) does the whole update for one number in a single shift.

What if you had to split into k groups with equal sums?

A single table is no longer enough. Use backtracking that fills k buckets of size total / k, placing big numbers first and skipping buckets that have the same current sum.

In Hinglish

Do barabar hisse matlab bas ek subset dhoondo jiska sum `total / 2` ho. `can[s]` table ko upar se neeche bharo, taaki har number ek hi baar use ho.

Related problems