DSADPSubset

Partition Equal Subset Sum Visualizer

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.

  • dp[s] is true if some subset sums to s.
  • Walk downward so each number is used once.
  • Target = sum/2. Reaching it is the whole problem.

Brute force approach

Assign each number to the left or right subset. 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.

Try a small input

At most 4 numbers. 1 5 11 5 sums to 22, and half is 11, so the answer is 1.

Answer: 0 Brute calls: 0 Memo calls: 0 Calls saved: 0
Recursive brute force
def can(i, rem):
if rem == 0: return yes
return can(i+1, rem-nums[i]) or can(i+1, rem)
Step 0

Memoized approach

If the total is even, ask whether a subset reaches 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.

Memoized recursion
def can(i, rem, memo):
if (i, rem) in memo: return memo
if rem == 0: return yes
memo = take or skip
return memo
Step 0

How this visualizer works

Array 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

dp_table.state

Input
DP table
Cell being filled
Source / window
Already computed

current_action

Load an input, then press Play or Step.

live_trace.py — DP fill

1def canPartition(nums):
2s = sum(nums)
3if s % 2: return 0
4t, dp = s//2, [False]*(s//2+1)
5dp[0] = True
6for x in nums:
7for i in range(t, x-1, -1):
8dp[i] = dp[i] or dp[i-x]
9return 1 if dp[t] else 0

stdout — decision log

// each table write is recorded here
Speed Ready

Pseudocode & Complexity

dp_recurrence.txt

1def canPartition(nums):
2s = sum(nums)
3if s % 2: return 0
4t, dp = s//2, [False]*(s//2+1)
5dp[0] = True
6for x in nums:
7for i in range(t, x-1, -1):
8dp[i] = dp[i] or dp[i-x]
9return 1 if dp[t] else 0

complexity.md

OperationTime
Each (num, sum)O(1)
FillO(n · sum/2)
SpaceO(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

dp.py

1def partition_brute(nums, i=0, left=0, right=0):
2if i == len(nums):
3return 1 if left == right else 0
4take_left = partition_brute(nums, i + 1, left + nums[i], right)
5if take_left:
6return 1
7return partition_brute(nums, i + 1, left, right + nums[i])
1def partition_dp(nums):
2total = sum(nums)
3if total % 2 == 1:
4return 0
5target = total // 2
6dp = [False] * (target + 1)
7dp[0] = True
8for x in nums:
9for s in range(target, x - 1, -1):
10if dp[s - x]:
11dp[s] = True
12return 1 if dp[target] else 0
1int partition_brute(int *nums, int n, int i, int left, int right) {
2if (i == n) {
3return left == right ? 1 : 0;
4}
5if (partition_brute(nums, n, i + 1, left + nums[i], right)) {
6return 1;
7}
8return partition_brute(nums, n, i + 1, left, right + nums[i]);
9}
1int partition_dp(int *nums, int n) {
2int total = 0;
3int i;
4int s;
5int target;
6int dp[10001];
7for (i = 0; i < n; i++) {
8total += nums[i];
9}
10if (total % 2 == 1) {
11return 0;
12}
13target = total / 2;
14for (s = 0; s <= target; s++) {
15dp[s] = 0;
16}
17dp[0] = 1;
18for (i = 0; i < n; i++) {
19for (s = target; s >= nums[i]; s--) {
20if (dp[s - nums[i]]) {
21dp[s] = 1;
22}
23}
24}
25return dp[target];
26}