Solution Equal Subset Sum Partition
Problem Statement
Given a set of positive numbers, find if we can partition it into two subsets such that the sum of elements in both the subsets is equal.
Example 1:
Input: {1, 2, 3, 4}
Output: True
Explanation: The given set can be partitioned into two subsets with equal sum: {1, 4} & {2, 3}
Example 2:
Input: {1, 1, 3, 4, 7}
Output: True
Explanation: The given set can be partitioned into two subsets with equal sum: {1, 3, 4} & {1, 7}
Example 3:
Input: {2, 3, 4, 6}
Output: False
Explanation: The given set cannot be partitioned into two subsets with equal sum.
Constraints:
1 <= num.length <= 2001 <= num[i] <= 100
Basic Solution
This problem follows the 0/1 Knapsack pattern. A basic brute-force solution could be to try all combinations of partitioning the given numbers into two sets to see if any pair of sets has an equal sum.
Assume if S represents the total sum of all the given numbers, then the two equal subsets must have a sum equal to S/2. This essentially transforms our problem to: "Find a subset of the given numbers that has a total sum of S/2".
So our brute-force algorithm will look like:
for each number 'i' create a new set which INCLUDES number 'i' if it does not exceed 'S/2', and recursively process the remaining numbers create a new set WITHOUT number 'i', and recursively process the remaining items return true if any of the above sets has a sum equal to 'S/2', otherwise return false
Code
Here is the code for the brute-force solution:
public class Solution {
public boolean canPartition(int[] num) {
int sum = 0;
for (int i = 0; i < num.length; i++) sum += num[i];
// if 'sum' is a an odd number, we can't have two subsets with equal sum
if (sum % 2 != 0) return false;
return this.canPartitionRecursive(num, sum / 2, 0);
}
private boolean canPartitionRecursive(int[] num, int sum, int currentIndex) {
// base check
if (sum == 0) return true;
if (num.length == 0 || currentIndex >= num.length) return false;
// recursive call after choosing the number at the currentIndex
// if the number at currentIndex exceeds the sum, we shouldn't process this
if (num[currentIndex] <= sum) {
if (
canPartitionRecursive(num, sum - num[currentIndex], currentIndex + 1)
) return true;
}
// recursive call after excluding the number at the currentIndex
return canPartitionRecursive(num, sum, currentIndex + 1);
}
}
The time complexity of the above algorithm is exponential
Top-down Dynamic Programming with Memoization
We can use memoization to overcome the overlapping sub-problems. As stated in previous lessons, memoization is when we store the results of all the previously solved sub-problems return the results from memory if we encounter a problem that has already been solved.
Since we need to store the results for every subset and for every possible sum, therefore we will be using a two-dimensional array to store the results of the solved sub-problems. The first dimension of the array will represent different subsets and the second dimension will represent different 'sums' that we can calculate from each subset. These two dimensions of the array can also be inferred from the two changing values (sum and currentIndex) in our recursive function canPartitionRecursive().
Code
Here is the code for Top-down DP with memoization:
public class Solution {
public boolean canPartition(int[] num) {
int sum = 0;
for (int i = 0; i < num.length; i++) sum += num[i];
// if 'sum' is a an odd number, we can't have two subsets with equal sum
if (sum % 2 != 0) return false;
Boolean[][] dp = new Boolean[num.length][sum / 2 + 1];
return this.canPartitionRecursive(dp, num, sum / 2, 0);
}
private boolean canPartitionRecursive(
Boolean[][] dp,
int[] num,
int sum,
int currentIndex
) {
// base check
if (sum == 0) return true;
if (num.length == 0 || currentIndex >= num.length) return false;
// if we have not already processed a similar problem
if (dp[currentIndex][sum] == null) {
// recursive call after choosing the number at the currentIndex
// if the number at currentIndex exceeds the sum, we shouldn't process this
if (num[currentIndex] <= sum) {
if (
canPartitionRecursive(
dp,
num,
sum - num[currentIndex],
currentIndex + 1
)
) {
dp[currentIndex][sum] = true;
return true;
}
}
// recursive call after excluding the number at the currentIndex
dp[currentIndex][sum] = canPartitionRecursive(
dp,
num,
sum,
currentIndex + 1
);
}
return dp[currentIndex][sum];
}
}
The above algorithm has time and space complexity of
Bottom-up Dynamic Programming
Let's try to populate our dp[][] array from the above solution, working in a bottom-up fashion. Essentially, we want to find if we can make all possible sums with every subset. This means, dp[i][s] will be 'true' if we can make sum 's' from the first 'i' numbers.
So, for each number at index 'i' (0 <= i < num.length) and sum 's' (0 <= s <= S/2), we have two options:
- Exclude the number. In this case, we will see if we can get 's' from the subset excluding this number:
dp[i-1][s] - Include the number if its value is not more than 's'. In this case, we will see if we can find a subset to get the remaining sum:
dp[i-1][s-num[i]]
If either of the two above scenarios is true, we can find a subset of numbers with a sum equal to 's'.
Let's start with our base case of zero capacity:





From the above visualization, we can clearly see that it is possible to partition the given set into two subsets with equal sums, as shown by bottom-right cell: dp[3][5] => T
Code
Here is the code for our bottom-up dynamic programming approach:
public class Solution {
public boolean canPartition(int[] num) {
int n = num.length;
// find the total sum
int sum = 0;
for (int i = 0; i < n; i++) sum += num[i];
// if 'sum' is a an odd number, we can't have two subsets with same total
if (sum % 2 != 0) return false;
// we are trying to find a subset of given numbers that has a total sum of ‘sum/2’.
sum /= 2;
boolean[][] dp = new boolean[n][sum + 1];
// populate the sum=0 column, as we can always have '0' sum without including any element
for (int i = 0; i < n; i++) dp[i][0] = true;
// with only one number, we can form a subset only when the required sum is equal to its value
for (int s = 1; s <= sum; s++) {
dp[0][s] = (num[0] == s ? true : false);
}
// process all subsets for all sums
for (int i = 1; i < n; i++) {
for (int s = 1; s <= sum; s++) {
// if we can get the sum 's' without the number at index 'i'
if (dp[i - 1][s]) {
dp[i][s] = dp[i - 1][s];
} else if (s >= num[i]) { // else if we can find a subset to get the remaining sum
dp[i][s] = dp[i - 1][s - num[i]];
}
}
}
// the bottom-right corner will have our answer.
return dp[n - 1][sum];
}
}
The above solution has time and space complexity of
🤖 Don't fully get this? Learn it with Claude
Stuck on Solution Equal Subset Sum Partition? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.
Progressively stronger hints — you still solve it.
I'm working on the problem **Solution Equal Subset Sum Partition** (DSA). Give me a HINT LADDER: start with the tiniest nudge, then wait. Only reveal the next, stronger hint when I ask. Do NOT show the full solution unless I type 'show solution'. Keep me doing the thinking. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
See the technique, not just code.
Explain the optimal approach to **Solution Equal Subset Sum Partition** with a VISUAL walkthrough: trace it on a small concrete example using ASCII art / a step-by-step diagram, narrate what changes each step, then give time & space complexity with a one-line derivation. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
Catch bugs, edge cases, sub-optimality.
I'll paste my solution to **Solution Equal Subset Sum Partition**. Review it for correctness, missed edge cases, and time/space complexity, then coach me toward the optimal — don't just rewrite it. Ask me to paste my code now. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
Lock in recognition with look-alikes.
Give me 2 problems that use the SAME underlying pattern as **Solution Equal Subset Sum Partition**. For each, let me attempt first, then review my answer and name the trigger signal that reveals the pattern. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.