CJ

Explore

Tracks

My Courses

Edpresso

Refer a Friend

Create

Grokking Dynamic Programming Patterns for Coding Interviews

97% completed

Introduction

What is Dynamic Programming?

Pattern 1: 0/1 Knapsack

0/1 Knapsack

Equal Subset Sum Partition

Subset Sum

Minimum Subset Sum Difference

Count of Subset Sum

Target Sum

Pattern 2: Unbounded Knapsack

Unbounded Knapsack

Rod Cutting

Coin Change

Minimum Coin Change

Maximum Ribbon Cut

Pattern 3: Fibonacci Numbers

Fibonacci numbers

Staircase

Number factors

Minimum jumps to reach the end

Minimum jumps with fee

House thief

Pattern 4: Palindromic Subsequence

Longest Palindromic Subsequence

Longest Palindromic Substring

Count of Palindromic Substrings

Minimum Deletions in a String to make it a Palindrome

Palindromic Partitioning

Pattern 5: Longest Common Substring

Longest Common Substring

Longest Common Subsequence

Minimum Deletions & Insertions to Transform a String into another

Longest Increasing Subsequence

Maximum Sum Increasing Subsequence

Shortest Common Super-sequence

Minimum Deletions to Make a Sequence Sorted

Longest Repeating Subsequence

Subsequence Pattern Matching

Longest Bitonic Subsequence

Longest Alternating Subsequence

Edit Distance

Strings Interleaving

Appendix

Contact Us

Other courses

Mark Course as Completed

Equal Subset Sum Partition

We'll cover the following

Problem Statement
*

Example 1:

Example 2:

Example 3:

Try it yourself

Basic Solution

Code

Top-down Dynamic Programming with Memoization

Code

Bottom-up Dynamic Programming

Code

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.

Try it yourself #

This problem looks similar to the 0/1 Knapsack problem, try solving it before moving on to see the solution:

Java

Java

Java

JS

JS

Python3

Python3

C++

C++

class PartitionSet {

static boolean canPartition(int[] num) {
//TODO: Write - Your - Code
return false;
}
}

TEST

SAVERESET

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:

Java

Java

Java

JS

JS

Python3

Python3

C++

C++

class PartitionSet {

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);
}

public static void main(String[] args) {
PartitionSet ps = new PartitionSet();
int[] num = {1, 2, 3, 4};
System.out.println(ps.canPartition(num));
num = new int[]{1, 1, 3, 4, 7};
System.out.println(ps.canPartition(num));
num = new int[]{2, 3, 4, 6};
System.out.println(ps.canPartition(num));
}
}

RUN

SAVERESET

The time complexity of the above algorithm is exponential O(2n)O(2^n)O(2​n​​), where ‘n’ represents the total number. The space complexity is O(n)O(n)O(n), this memory which will be used to store the recursion stack.

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:

Java

Java

Java

JS

JS

Python3

Python3

C++

C++

class PartitionSet {

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];
}

public static void main(String[] args) {
PartitionSet ps = new PartitionSet();
int[] num = { 1, 2, 3, 4 };
System.out.println(ps.canPartition(num));
num = new int[] { 1, 1, 3, 4, 7 };
System.out.println(ps.canPartition(num));
num = new int[] { 2, 3, 4, 6 };
System.out.println(ps.canPartition(num));
}
}

RUN

SAVERESET

The above algorithm has time and space complexity of O(N∗S)O(N*S)O(N∗S), where ‘N’ represents total numbers and ‘S’ is the total sum of all the numbers.

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:

'0' sum can always be found through an empty set

1 of 10

With only one number, we can form a subset only when the required sum is equal to its value

2 of 10

sum: 1, index:1=> (dp[index-1][sum] , as the 'sum' is less than the number at index '1'

3 of 10

sum: 2, index:1=> (dp[index-1][sum] || dp[index-1][sum-2])

4 of 10

sum: 3, index:1=> (dp[index-1][sum] || dp[index-1][sum-2])

5 of 10

sum: 4,5, index:1=> (dp[index-1][sum] || dp[index-1][sum-2])

6 of 10

sum: 1,2,3, index:2=> (dp[index-1][sum] || dp[index-1][sum-3])

7 of 10

sum: 4, index:2=> (dp[index-1][sum] || dp[index-1][sum-3])

8 of 10

sum: 5, index:2=> (dp[index-1][sum] || dp[index-1][sum-3])

9 of 10

sum: 1-5, index:3=> (dp[index-1][sum] || dp[index-1][sum-4])

10 of 10

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:

Java

Java

Java

JS

JS

Python3

Python3

C++

C++

class PartitionSet {

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];
}

public static void main(String[] args) {
PartitionSet ps = new PartitionSet();
int[] num = {1, 2, 3, 4};
System.out.println(ps.canPartition(num));
num = new int[]{1, 1, 3, 4, 7};
System.out.println(ps.canPartition(num));
num = new int[]{2, 3, 4, 6};
System.out.println(ps.canPartition(num));
}
}

RUN

SAVERESET

The above solution has time and space complexity of O(N∗S)O(N*S)O(N∗S), where ‘N’ represents total numbers and ‘S’ is the total sum of all the numbers.

Completed

←    Back
0/1 Knapsack

Next    →
Subset Sum

Report an Issue

Ask a Question