#416

Partition Equal Subset Sum

Medium 0/1 knapsack (subset sum)

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

Decide whether the array can be split into two subsets with equal sums.

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: can the numbers be split into two equal-sum halves?

Why this works: find a subset summing to total / 2 — 0/1 knapsack with a boolean array, looping sums downward so each number is used once.

Example: [1, 5, 11, 5], target 11

reachable after 1: {0,1}; after 5: {0,1,5,6}; after 11: {…,11} ✓ → true

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Looping s upwards, which lets the same number be used many times (that's unbounded knapsack).

Solve on LeetCode ↗
Complexity
  • Time: O(n · target).
  • Space: O(target).
Topic: Dynamic Programming
🎬 Episodes 86–96: video coming soon