The subset sum family
35 min · Advanced
Need help? Ask a tutor on WhatsApp (opens in a new tab)The subset sum family includes partition equal subset sum, target sum, and counting subsets. We start with plain recursion to see how these problems reduce to 0/1 knapsack.
Brute Force Recursion and the Knapsack Connection
Subset sum asks whether some of the given numbers, each used at most once, add up exactly to a target value.
Given an array of numbers, is there a subset that sums up to target T? With plain recursion, for each number we branch into two choices: include it in the sum, or skip it.
Example 1 · Subset Sum Feasibility Check
public class SubsetSumDemo { public static boolean canPartition(int[] nums, int target) { boolean[] dp = new boolean[target + 1]; dp[0] = true; for (int num : nums) { for (int s = target; s >= num; s--) { dp[s] = dp[s] || dp[s - num]; } } return dp[target]; }
public static void main(String[] args) { int[] nums = {3, 34, 4, 12, 5, 2}; System.out.println("Can reach 9: " + canPartition(nums, 9)); System.out.println("Can reach 30: " + canPartition(nums, 30)); }}Can reach 9: true Can reach 30: false
What this code does
- Initializes boolean array with dp[0] = true.
- Iterates backward from target down to num for each element.
- Sum 9 is achievable via 4 + 5, while 30 cannot be formed.
This generates all 2^n subsets. For an array of 35 numbers, 2^35 is over 34 billion operations. Yet subsets like {2, 3} and {5} both produce a partial sum of 5, repeating all subsequent checks for remaining target T - 5. Subset sum is actually 0/1 knapsack in disguise: each number's weight is its value, and the backpack capacity is target T.
Partition Equal Subset Sum
Can an array be split into two subsets with equal sums? First, sum all elements. If the total sum is odd, it is impossible to split it equally into two integers, so we immediately return false.
Example 2 · Partition Equal Subset Sum
public class PartitionEqualDemo { public static boolean canPartition(int[] nums) { int sum = 0; for (int x : nums) sum += x; if (sum % 2 != 0) return false; int target = sum / 2; boolean[] dp = new boolean[target + 1]; dp[0] = true; for (int num : nums) { for (int s = target; s >= num; s--) { dp[s] = dp[s] || dp[s - num]; } } return dp[target]; }
public static void main(String[] args) { int[] a1 = {1, 5, 11, 5}; System.out.println("Can partition {1, 5, 11, 5}: " + canPartition(a1)); int[] a2 = {1, 2, 3, 5}; System.out.println("Can partition {1, 2, 3, 5}: " + canPartition(a2)); }}Can partition {1, 5, 11, 5}: true
Can partition {1, 2, 3, 5}: falseWhat this code does
- Sum of set 1 is 22; target is 11. Subset {1, 5, 5} reaches 11.
- Sum of set 2 is 11, which is odd, so equal partition is impossible.
- Runs in O(n * target) time and O(target) auxiliary space.
If total sum is even, the target is simply total / 2. If we can find any subset that sums to target, the remaining elements automatically sum to target as well. We run the 1D boolean subset sum DP: dp[w] = dp[w] || dp[w - num], iterating w backwards from target down to num in O(n * target) time.
Counting Subsets with Given Sum and Handling Zeros
Counting subsets asks how many subsets achieve the target sum, instead of whether one exists. The recurrence changes from logical OR to addition: dp[w] = dp[w] + dp[w - num].
public class CountSubsets { public static void main(String[] args) { int[] nums = {0, 0, 1}; int target = 1; int[] dp = new int[target + 1]; dp[0] = 1; // the empty subset for (int num : nums) { for (int w = target; w >= num; w--) { dp[w] += dp[w - num]; } } System.out.println("subsets with sum 1: " + dp[target]); // {1} x 2^2 zero choices }}subsets with sum 1: 4
Base case: dp[0] = 1, because the empty subset sums to 0. But beware of zeros in the input array!
Each zero in the array can either be included or excluded without changing the sum. If an array has z zeros, every valid subset can be paired with any subset of those zeros, multiplying the count by 2^z.
Target Sum: Algebraic Reduction
In Target Sum, you assign a '+' or '-' sign to each number to achieve target S. Brute force recursion branches 2^n times trying both signs for each number.
Example 3 · Target Sum Assignment Counting
public class TargetSumDemo { public static int findTargetSumWays(int[] nums, int target) { int sum = 0; for (int x : nums) sum += x; if ((sum + target) % 2 != 0 || sum < Math.abs(target)) return 0; int P = (sum + target) / 2; int[] dp = new int[P + 1]; dp[0] = 1; for (int num : nums) { for (int s = P; s >= num; s--) { dp[s] += dp[s - num]; } } return dp[P]; }
public static void main(String[] args) { int[] nums = {1, 1, 1, 1, 1}; System.out.println("Ways to achieve target 3: " + findTargetSumWays(nums, 3)); }}Ways to achieve target 3: 5
What this code does
- Reduces Target Sum to finding positive subset P = (5 + 3) / 2 = 4.
- Counting subsets summing to 4 with five 1s yields 5 distinct ways.
- Mathematical reduction eliminates exponential 2^n recursion.
We can reduce this algebraically: let P be the sum of positive numbers, and N be the sum of negative numbers. Then P - N = target, and P + N = total. Adding equations gives 2 * P = total + target, so P = (total + target) / 2. The problem becomes: count subsets whose sum equals (total + target) / 2! If total + target is odd or negative, return 0 immediately.
Next step
Check your understanding
10 quick questions