What is Partition Equal Subset Sum
Can the array be split into two subsets with the same sum? If the total is odd, no. Otherwise ask whether some subset reaches total/2 — 0/1 knapsack with boolean reachability.
Brute force approach
2^n assignments. Return whether any split ties.If the total is even, the question is whether a subset sums to half. Each number is taken or skipped. The pair (index, remaining) repeats, so memoization stores it.
At most 4 numbers. 1 5 11 5 sums to 22, and half is 11, so the answer is 1.
Memoized approach
sum/2. Boolean 0/1 knapsack, downward scan.Each state is solved once. A dashed node is a cache hit: the call returns immediately and that branch stops.
1, 5, 11, 5. Sum 22, target 11. After placing 11 the target bit is already true. Step through the DP table. The live code on the right is the DP recurrence — brute force is shown in Implementations below so you can see why it TLE.Interactive DP trace
Pseudocode & Complexity
| Operation | Time |
|---|---|
| Each (num, sum) | O(1) |
| Fill | O(n · sum/2) |
| Space | O(sum) |
Brute force explores the same subproblems many times and will TLE on the hidden tests. The DP codes below reuse each answer once.
Implementations