Subset Sum
Given an array of size n and a sum K, determine whether any subset is possible with that sum or not. Print "yes" if there is any subset present else print "no".
Input & output:
In the first line the input will be n and K, space separated.
The next line contains the array elements
Output will be "yes" or "no"
Example with explanation:
Example 1:
Input:
The array size be: 5
The sum be: 11
The Elements be: 5 6 7 3 9
Output:
Yes
Explanation:
5 & 6 sums to 11. Thus the subset can be [5, 6] and output will be "yes".
Example 2:
Input:
The array size be: 5
The sum be: 11
The Elements be: 5 6 7 3 9
Output:
Yes
Explanation:
5 & 6 sums to 11. Thus the subset can be [5, 6] and output will be "yes".
Of course the naïve solution would be to generate all possible subsets and check their sum equals to K or not. But it would of course be computationally exponential to generate all possible subsets.
To do so, the recursion would be:
For nth element the cases are:
So, the recursion function can be written as:
Where,
Now base case would be,
Here comes the concept of sub problem. Initially the problem is for size n and sum K. Then it gets reduced to {size (n-1) and sum K} or {size (n-1) and (sum K-arr[n-1])}
So if you draw the recursion tree there will be many such sub-problem which will be overlapping ones. That means we will re-compute same sub-problems again and again. That’s why we need to store the value of sub-problems and use dynamic programming.
Converting to dynamic programming:
1) Initialize dp[n+1][sum+1] which is a Boolean table to false 2) Convert the base cases for recursion for i=1 to sum dp[0][i]=false; for i=0 to n dp[i][0]=true; 3) Build the table as per the recursion formula. for i=1 to sum for j=1 to n //if sub-sum i>jth element then only we can take that if(i>=arr[j-1]) //consider both case mentioned in solution approach dp[j][i]=dp[j-1][i] | dp[j-1][i-arr[j-1]]; else // don't take jth element dp[j][i]=dp[j-1][i]; if(i==sum) if(dp[j][i]){ return true; End If end for end for 4) If not returned from the loop return false;C++ Implementation:
Output
need an explanation for this answer? contact us directly to get an explanation for this answer