# Subset Sum Problem using Dynamic Programming

«
»

This is a C++ Program that Solves Subset Sum Problem using Dynamic Programming technique.

Problem Description

There is a subset A of n positive integers and a value sum. Find whether or not there exists any subset of the given set, the sum of whose elements is equal to the given value of sum.

Problem Solution

This problem can be solved using Dynamic programming. We will proceed with finding whether there exists any subset of sum 1, then for sum 2 and so on. We will keep storing the values in a matrix to avoid recomputation.

Time complexity of this solution is O(n*Sum).

Expected Input and Output

Case-1:

```sum=17
n=4
A[]={2,4,6,9}

Required subset exists
subset {2,6,9} has the sum 17```

Case-2:

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!
```sum=17
n=4
A[]={2,4,6,8}

No Subset found with required sum```
Program/Source Code

Here is source code of the C++ Program to Solve Subset Sum Problem. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

1. `#include<iostream>`
2. `using namespace std;`
3. ` `
4. `bool subset_sum(int a[],int n, int sum)`
5. `{`
6. `    //boolean matrix to store results`
7. `    bool dp[n+1][sum+1];`
8. ` `
9. `    //dp[i][j]=whethere there is a subset of sum j in subarray a[0....i-1]`
10. ` `
11. `    int i,j;`
12. ` `
13. `    //initialization`
14. `    //for sum=0, there is always a subset possible ie., the empty set`
15. `    for(i=0;i<=n;i++)`
16. `        dp[i]=true;`
17. ` `
18. `    //if there are no elements in the array, no subset is possible for a non-zero sum`
19. `    for(j=1;j<=sum;j++)`
20. `        dp[j]=false;`
21. ` `
22. `    //i represents the no. of elements of array considered`
23. `    for(i=1;i<=n;i++)`
24. `    {`
25. `        //j represents the sum of subset being searched for`
26. `        for(j=1;j<=sum;j++)`
27. `        {`
28. `            //if using i-1 elements, there is a subset of desired sum`
29. `            //no need to search further`
30. `            //the result is true `
31. `            if(dp[i-1][j]==true)`
32. `                dp[i][j]=true;`
33. ` `
34. `            //otherwise, we will inspect`
35. `            else`
36. `            {`
37. `                //if value of current element is greater than the required sum`
38. `                //this element cannot be considered`
39. `                if(a[i-1]>j)`
40. `                    dp[i][j]=false;`
41. ` `
42. `                //check that after including this element, Is there any subset present for the remaining sum ie., j-a[i-1]`
43. `                else`
44. `                    dp[i][j]=dp[i-1][j-a[i-1]];`
45. `            }`
46. `        }`
47. `    }`
48. ` `
49. `    //return the overall result`
50. `    return dp[n][sum];`
51. `}`
52. ` `
53. `int main()`
54. `{`
55. `    int i;`
56. `    int n;`
57. `    int sum;`
58. ` `
59. `    cout<<"Enter the value of sum"<<endl;`
60. `    cin>>sum;`
61. ` `
62. `    cout<<"Enter the number of elements in the set"<<endl;`
63. `    cin>>n;`
64. `    int a[n];`
65. ` `
66. `    cout<<"Enter the values"<<endl;`
67. `    for(i=0;i<n;i++)`
68. `        cin>>a[i];`
69. ` `
70. `    bool result=subset_sum(a,n,sum);`
71. ` `
72. `    if(result==true)`
73. `        cout<<"subset with the given sum found";`
74. ` `
75. `    else`
76. `        cout<<"no required subset found";`
77. ` `
78. `    cout<<endl; `
79. `    return 0;`
80. `}`
Program Explanation

In the main function, we ask the user to input number of elements in the set and the values of all elements. We pass these values to the function subsetSum as parameter. This function will calculate the expected result and return it. The result will be displayed on the basis of the returned value.

Runtime Test Cases
```
Case-1:
\$ g++ subset_sum.cpp
\$ ./a.out
Enter the value of sum
17
Enter the number of elements in the set
4
Enter the values
2
4
6
9
subset with the given sum found```

Sanfoundry Global Education & Learning Series – Dynamic Programming Problems.
To practice all Dynamic Programming Problems, here is complete set of 100+ Problems and Solutions. 