Skip to content
StackDecode
Dynamic programmingAnimated

Partition Equal Subset Sum

Split into two equal-sum halves: subset sum with target sum/2.

Time

O(n · sum)

Extra space

O(sum)

Use it for

Fair splitting.

How it works

  1. Can the numbers be split into two groups with the same sum?
  2. If the total is odd, impossible. Otherwise we need a subset adding to total / 2 (the rest then adds to the same).
  3. That is the subset-sum problem with target = total / 2.
  4. Update a reachable-sums row once per number. O(n × total).

Watch it run

Loading animation…