Partition Equal Subset Sum

Medium

Topics
ArrayDynamic Programming

Given an array nums of positive integers, return true if it can be split into two subsets with equal sum.

Example 1

105111253
one half, summing to 11
The total is 22, so each half must reach 11 — and the shaded cells do. This is subset-sum: a boolean row indexed by target, swept right to left per value so that each value is used at most once.
Cells are the array values; the small numbers underneath are indices.
Input:  nums = [1,5,11,5]
Output: true

Example 2

10213253
The total is 11, which is odd — so it cannot split into two equal halves and the answer is false before any searching. Checking the parity of the sum first is the cheap early exit.
Input:  nums = [1,2,3,5]
Output: false

Constraints

  • 1 <= nums.length <= 200
  • 1 <= nums[i] <= 100
Editorial — intuition, approach, complexity and pitfalls

Intuition

Two subsets with equal sums means each holds exactly half the total, so an odd total is an immediate no. What remains is: can some subset reach total / 2? That is subset-sum, and the classic 0/1 knapsack over a boolean reachability array answers it.

dp[j] means "some subset of the items considered so far sums to j". Each item is offered once. The problem is NP-hard in the item values, but the constraints bound the total, which is exactly what makes the pseudo-polynomial table affordable.

Approach

  1. Return false if the total is odd; otherwise set t = total / 2.
  2. Start with dp[0] = true and everything else false.
  3. For each item x, walk j from t down to x and set dp[j] if dp[j - x] holds.
  4. Return dp[t].

Dry Run

nums = [1,5,11,5]      total = 22 is even, so t = 11

  after item    reachable sums
  (none)        [0]
  1             [0, 1]
  5             [0, 1, 5, 6]
  11            [0, 1, 5, 6, 11]
  5             [0, 1, 5, 6, 10, 11]

dp[11] = true   ->  {11} and {1,5,5} both sum to 11

Complexity

  • Time — O(n · sum): with n <= 200 and values at most 100, the total is at most 20,000 and t at most 10,000, so roughly 2×10⁶ updates.
  • Space — O(sum): one boolean row of length t + 1. The two-dimensional table is the natural first formulation and reduces to this row because each item reads only the previous state.

Common Pitfalls

  • Walking the inner loop upward. That lets a single item be consumed more than once — it turns 0/1 knapsack into unbounded knapsack. On [1,2,5] the ascending version reports true by using the 1 four times; the correct answer is false.
  • Dropping the odd-total check. Integer division then truncates the half and the table answers a different question without complaining.
  • Calling the problem NP-hard and stopping. It is — in the magnitude of the values — but the bounded total is precisely the condition under which the DP is the expected answer.
  • Returning dp[t] from a table sized to total rather than t, which doubles the work for no gain.
  • Missing the bitset form. bits |= bits << x with the answer read off bit t is the same algorithm with a word-sized constant-factor win, and it is a good thing to offer once the O(n · sum) version is on the board.

Full Solution — all languages

class Solution:
    def canPartition(self, nums):
        total = sum(nums)
        if total % 2:
            return False
        t = total // 2
        dp = [False] * (t + 1)
        dp[0] = True
        for x in nums:
            for j in range(t, x - 1, -1):
                if dp[j - x]:
                    dp[j] = True
        return dp[t]
Asked at Dream11 eBay
SensAI
Beta
Tap the mic to stop voice mode