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

Count of Subset Sum

We'll cover the following

Problem Statement
*

Example 1:

Example 2:

Basic Solution

Code

Top-down Dynamic Programming with Memoization

Code

Bottom-up Dynamic Programming

Code

Challenge

Problem Statement #

Given a set of positive numbers, find the total number of subsets whose sum is equal to a given number ‘S’.

Example 1: #

Input: {1, 1, 2, 3}, S=4
Output: 3
The given set has '3' subsets whose sum is '4': {1, 1, 2}, {1, 3}, {1, 3}
Note that we have two similar sets {1, 3}, because we have two '1' in our input.

Example 2: #

Input: {1, 2, 7, 1, 5}, S=9
Output: 3
The given set has '3' subsets whose sum is '9': {2, 7}, {1, 7, 1}, {1, 2, 1, 5}

Basic Solution #

This problem follows the 0/1 Knapsack pattern and is quite similar to Subset Sum. The only difference in this problem is that we need to count the number of subsets, whereas in the Subset Sum we only wanted to know if there exists a subset with the given sum.

A basic brute-force solution could be to try all subsets of the given numbers to count the subsets that have a sum equal to ‘S’. 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', and recursively
process the remaining numbers and sum
create a new set without number 'i', and recursively process the remaining numbers
return the count of subsets who has a sum equal to 'S'

Code #

Here is the code for the brute-force solution:

Java

Java

Java

JS

JS

Python3

Python3

C++

C++

class SubsetSum {

public int countSubsets(int[] num, int sum) {
return this.countSubsetsRecursive(num, sum, 0);
}

private int countSubsetsRecursive(int[] num, int sum, int currentIndex) {
// base checks
if (sum == 0)
return 1;

if(num.length == 0 || currentIndex >= num.length)
return 0;

// recursive call after selecting the number at the currentIndex
// if the number at currentIndex exceeds the sum, we shouldn't process this
int sum1 = 0;
if( num[currentIndex] <= sum )
sum1 = countSubsetsRecursive(num, sum - num[currentIndex], currentIndex + 1);

// recursive call after excluding the number at the currentIndex
int sum2 = countSubsetsRecursive(num, sum, currentIndex + 1);

return sum1 + sum2;
}

public static void main(String[] args) {
SubsetSum ss = new SubsetSum();
int[] num = {1, 1, 2, 3};
System.out.println(ss.countSubsets(num, 4));
num = new int[]{1, 2, 7, 1, 5};
System.out.println(ss.countSubsets(num, 9));
}
}

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 is used to store the recursion stack.

Top-down Dynamic Programming with Memoization #

We can use memoization to overcome the overlapping sub-problems. We will be using a two-dimensional array to store the results of solved sub-problems. As mentioned above, we need to store results for every subset and for every possible sum.

Code #

Here is the code:

Java

Java

Java

JS

JS

Python3

Python3

C++

C++

class SubsetSum {

public int countSubsets(int[] num, int sum) {
Integer[][] dp = new Integer[num.length][sum + 1];
return this.countSubsetsRecursive(dp, num, sum, 0);
}

private int countSubsetsRecursive(Integer[][] dp, int[] num, int sum, int currentIndex) {
// base checks
if (sum == 0)
return 1;

if(num.length == 0 || currentIndex >= num.length)
return 0;

// check 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
int sum1 = 0;
if( num[currentIndex] <= sum )
sum1 = countSubsetsRecursive(dp, num, sum - num[currentIndex], currentIndex + 1);

// recursive call after excluding the number at the currentIndex
int sum2 = countSubsetsRecursive(dp, num, sum, currentIndex + 1);

dp[currentIndex][sum] = sum1 + sum2;
}

return dp[currentIndex][sum];
}

public static void main(String[] args) {
SubsetSum ss = new SubsetSum();
int[] num = {1, 1, 2, 3};
System.out.println(ss.countSubsets(num, 4));
num = new int[]{1, 2, 7, 1, 5};
System.out.println(ss.countSubsets(num, 9));
}
}

RUN

SAVERESET

Bottom-up Dynamic Programming #

We will try to find if we can make all possible sums with every subset to populate the array db[TotalNumbers][S+1].

So, at every step we have two options:

Exclude the number. Count all the subsets without the given number up to the given sum => dp[index-1][sum]

Include the number if its value is not more than the ‘sum’. In this case, we will count all the subsets to get the remaining sum => dp[index-1][sum-num[index]]

To find the total sets, we will add both of the above two values:

dp[index][sum] = dp[index-1][sum] + dp[index-1][sum-num[index]])

Let’s start with our base case of size zero:

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

1 of 12

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

2 of 12

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

3 of 12

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

4 of 12

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

5 of 12

sum: 1, index:2=> dp[index-1][sum], as sum is less than the number at index 2 (i.e., 1 < 2)

6 of 12

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

7 of 12

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

8 of 12

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

9 of 12

sum: 1,2, index:3=> dp[index-1][sum] , as the sum is less than the element at index '3'

10 of 12

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

11 of 12

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

12 of 12

Code #

Here is the code for our bottom-up dynamic programming approach:

Java

Java

Java

JS

JS

Python3

Python3

C++

C++

class SubsetSum {

public int countSubsets(int[] num, int sum) {
int n = num.length;
int[][] dp = new int[n][sum + 1];

// populate the sum=0 columns, as we will always have an empty set for zero sum
for(int i=0; i < n; i++)
dp[i][0] = 1;

// 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 ? 1 : 0);
}

// process all subsets for all sums
for(int i=1; i < num.length; i++) {
for(int s=1; s <= sum; s++) {
// exclude the number
dp[i][s] = dp[i-1][s];
// include the number, if it does not exceed the sum
if(s >= num[i])
dp[i][s] += dp[i-1][s-num[i]];
}
}

// the bottom-right corner will have our answer.
return dp[num.length-1][sum];
}

public static void main(String[] args) {
SubsetSum ss = new SubsetSum();
int[] num = {1, 1, 2, 3};
System.out.println(ss.countSubsets(num, 4));
num = new int[]{1, 2, 7, 1, 5};
System.out.println(ss.countSubsets(num, 9));
}
}

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 desired sum.

Challenge #

Can we further improve our bottom-up DP solution? Can you find an algorithm that has O(S)O(S)O(S) space complexity?

Show Hint

Java

Java

Java

JS

JS

Python3

Python3

C++

C++

class SubsetSum {
static int countSubsets(int[] num, int sum) {
int n = num.length;
int[] dp = new int[sum + 1];
dp[0] = 1;

// 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[s] = (num[0] == s ? 1 : 0);
}

// process all subsets for all sums
for(int i=1; i < num.length; i++) {
for(int s=sum; s >= 0; s--) {
if(s >= num[i])
dp[s] += dp[s-num[i]];
}
}

return dp[sum];
}
}

TESTShow Solution

SAVERESET

Completed

←    Back
Minimum Subset Sum Difference

Next    →
Target Sum

Report an Issue

Ask a Question