1. Total sum
Equal partition requires splitting the total in half.
const total = nums.reduce((sum, num) => sum + num, 0)Typorithm · medium
Step 1 of 8 · intro card
Dynamic Programming
Partition Equal Subset Sum turns equal partition into a subset-sum DP for half of the total.
If the total is odd, no equal split exists.
Otherwise, ask whether any subset reaches total / 2.
Reverse sum iteration makes each number single-use.
Target: total / 2.
State: dp[sum] is reachable.
Base: dp[0] = true.
Reverse loop prevents reuse.
function canPartition(nums) {
const total = nums.reduce((sum, num) => sum + num, 0);
if (total % 2 !== 0) return false;
const target = total / 2;
const dp = Array(target + 1).fill(false);
dp[0] = true;
for (const num of nums) {
for (let sum = target; sum >= num; sum--) {
dp[sum] = dp[sum] || dp[sum - num];
}
}
return dp[target];
}Connect each part of the code to the concept before you type.
Equal partition requires splitting the total in half.
const total = nums.reduce((sum, num) => sum + num, 0)Odd total cannot split into two equal integer sums.
if (total % 2 !== 0) return falseSum 0 is always reachable by choosing no numbers.
dp[0] = trueA sum is reachable if it already was or if num completes it.
dp[sum] = dp[sum] || dp[sum - num]Read the concept map and follow the code breakdown.
Type the implementation, repair mistakes and work through every recall exercise.
Use the final check without hints; revisit weak concepts in short practice sessions.
Algorithm lab · interactive
InputNumbers 1,5,5,11 · half sum 11
Follow temporary values, local state and supporting structures at this step.
↗ The 12-cell working table above is itself auxiliary memory. Input values are shown separately.
Input · not auxiliary memory
Numbers4 entries
Around this step
Small, reproducible examples · ← → step · Space play/pause · Home/End first/last state