Skip to content
AI360Xpert
Beta
Difficulty: Medium1-D Dynamic Programming

Partition Equal Subset Sum

Problem in Plain English

Split the array into two groups with the same total, which is possible exactly when some subset adds up to half of the overall sum. Return true when such a subset exists and false otherwise.

Problem Statement

Given an integer array nums, return true if the array can be split into two subsets whose sums are equal, and false otherwise. Every element must belong to exactly one of the two subsets.

Both subsets share the same sum, so each one must total exactly half of the whole array sum. When the total sum is odd, no equal integer split exists.

The task therefore reduces to deciding whether some subset of nums adds up to total / 2.

Constraints

  • 1≤nums.length≤2001 \leq \mathrm{nums.length} \leq 200.
  • 1≤nums[i]≤1001 \leq \mathrm{nums}[i] \leq 100.

Examples

Example 1

Input
nums = [1, 5, 11, 5]
Output
true

The total is 22. Select positions 0, 1 and 3 for [1, 5, 5], leaving [11]; both sums are 11. Selecting the single 11 also proves the same answer.

Example 2

Input
nums = [1, 2, 3, 5]
Output
false

The total is 11. Two equal integer sums would have to be 5.5 each, so parity alone rules out a partition.

Example 3

Input
nums = [2]
Output
false

The total is even and the target is 1, but the only subset sums are 0 and 2. Even total is necessary, not sufficient.

Example 4

Input
nums = [1, 2, 5]
Output
false

The total is 8 and the target is 4. Values 1 and 2 can make only 0, 1, 2 or 3, while 5 is too large. Reusing the same 1 or 2 would incorrectly create 4.

Intuition

If the total is 22, both groups must sum to 11. Finding just one subset of sum 11 is enough: every unused position forms the other group, whose sum is 22 - 11. In Example 1, [11] and [1, 5, 5] are both witnesses. Each array position can be selected once, even when its value repeats. Rather than remembering every subset, remember which totals those subsets can reach; different subsets with the same total have identical possibilities when the next number arrives.

Approaches

Recursive Subset Search

Solution Details

Reveal Recursive Subset Search: intuition, complexity, and code

Hints

Hint 1
If one group sums to S, what sum must its complement have? Start with the total and its parity.
Hint 2
An even total still needs a subset of half that total. At each position choose take or skip; equal values at different positions are separate choices.
Hint 3
Store a flag for each reachable sum, starting with sum 0. A new x extends an old sum j - x to j.
Hint 4
Compress to one row by scanning sums downward. Scanning upward would let a freshly updated entry reuse the same position.

Edge Cases

  • One value, such as [2], has no half-total subset even though the total is even.
  • Odd totals such as [1, 2, 3, 5] fail before either search or table allocation.
  • An even total with no target witness, such as [1, 2, 5], must return false; parity is only an initial filter.
  • Repeated values such as [2, 2, 2, 2] can be selected at distinct positions, yielding two groups of sum 4.
  • An element larger than target, such as 8 in [1, 2, 3, 8], cannot enter the target subset. The other values cannot reach 7 either.

Common Mistakes and Interview Tips

  • Scanning sums upward changes zero-one selection into unlimited reuse. On [1, 2, 5], repeatedly extending with the first 1 incorrectly marks target 4.
  • Initializing every table entry false, including dp[0], removes the empty-subset base case and prevents any sum becoming reachable.
  • Checking exhausted recursion before success rejects a solution that uses the last position, as the [1, 5, 5] branch of Example 1 does.
  • Greedy choice can strand the remainder: in [3, 3, 3, 4, 5, 6], target 12 is reachable as 5 + 4 + 3, but descending greedy chooses 6 + 5 and cannot fill the remaining 1.
  • Calling the numeric-target table polynomial in the number of input bits hides its pseudo-polynomial dependence on T; explain both the target bound and the descending-update invariant.

Key Takeaway

When the question is whether some subset hits an exact total, convert it to a target-sum check and track reachable sums with a 1D table scanned backwards.