Subset Sum Problem

Given a set of positive integers and an integer s, is there any non-empty subset whose sum to s.

 

For example,


Input:

set = { 7, 3, 2, 5, 8 }
sum = 14

Output: Yes

subset { 7, 2, 5 } sums to 14

 

 
Naive algorithm would be to cycle through all subsets of N numbers and, for every one of them, check if the subset sums to the right number. The running time is of order O(2NN), since there are 2N subsets and, to check each subset, we need to sum at most N elements.
 

A better exponential time algorithm uses Recursion. Subset sum can also be thought of as a special case of the 0-1 Knapsack problem. For each item, there are two possibilities –
 

1. We include current item in the subset and recurse for remaining items with remaining sum.
 
2. We exclude current item from subset and recurse for remaining items.
 

Finally, we return true if we get subset by including or excluding current item else we return false. The base case of the recursion would be when no items are left or sum becomes negative. We return true when sum becomes 0 i.e. subset is found.

 
Below is C++ and Java implementation of the idea:

C++

Download   Run Code

Output:

Yes

Java

Download   Run Code

Output:

Yes

 
The time complexity of above solution is exponential and auxiliary space used by the program is O(1)

 
The problem has an optimal substructure. That means the problem can be broken down into smaller, simple “subproblems”, which can further be divided into yet simpler, smaller subproblems until the solution becomes trivial. Above solution also exhibits overlapping subproblems. If we draw the recursion tree of the solution, we can see that the same sub-problems are getting computed again and again.

We know that problems having optimal substructure and overlapping subproblems can be solved by using Dynamic Programming, in which subproblem solutions are Memoized rather than computed again and again. Below Memoized version follows the top-down approach, since we first break the problem into subproblems and then calculate and store values.

 
Memoized C++/Java implementation –

C++

Download   Run Code

Output:

Yes

Java

Download   Run Code

Output:

Yes

 
The time complexity of above solution is O(n x sum). Auxiliary space used by the program is also O(n x sum).
 

 
We can also solve this problem in bottom-up manner. In the bottom-up approach, we solve smaller sub-problems first, then solve larger sub-problems from them. The following bottom-up approach computes T[i][j], for each 1 <= i <= n and 1 <= j <= sum, which is true if subset with sum j can be found using items up to first i items. It uses value of smaller values i and j already computed. It has the same asymptotic run-time as Memoization but no recursion overhead.

 
Below is C++ and Java implementation of the idea:

C++

Download   Run Code

Output:

Yes

Java

Download   Run Code

Output:

Yes

 
Thanks for reading.

Please use ideone or C++ Shell or any other online compiler link to post code in comments.
Like us? Please spread the word and help us grow. Happy coding 🙂
 






Leave a Reply

avatar
  Subscribe  
newest oldest most voted
Notify of
Surya
Guest
Surya

Wonderful

Surbhi Jain
Guest
Surbhi Jain

Hi,
I am not able to figure out where is the property of Overlapping subproblem in this problem. Would this property always be true here or only in same cases. Where are the subproblems getting overlapped?
Thanks

anshul
Guest
anshul

HI – very nice explanation. One question – in the bottom up approach, why do we do this:
// if sum is zero
for (int i = 0; i <= n; i++)
T[i][0] = true;

If you could explain the reasoning – my understanding is since sum is 0, means there cannot be any subset that can sum up to 0 – so should be false.

anshul
Guest
anshul

I think I got it – the table is storing the results bottom up – so when the sum is zero – means we found a subset where item values equal to 0, so we return true. Correct?

anshul
Guest
anshul

* correction … sum – (item values) equal to 0