-
Notifications
You must be signed in to change notification settings - Fork 492
/
SubsetSum.cpp
69 lines (51 loc) Β· 1.79 KB
/
SubsetSum.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*Program to check if a Subset with the given sum
is present in the given array or not using dynamic programming
*/
#include<bits/stdc++.h>
using namespace std;
bool checkSubsetSum(int a[],int n, int sum)
{
bool dp[n+1][sum+1]; // A dp matrix to store the answers of the sub Problems
// Base Cases
for(int i = 0; i <= n; i++)
{
dp[i][0]=true; // Filling up the first row of the dp matrix with true
}
for(int j = 1; j <= sum; j++)
{
dp[0][j]=false; // Filling up the first column of the dp matrix with false
}
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= sum; j++)
{
if(j < a[i-1]) // if the number present at a[i-1] is greater than the sum
dp[i][j]=dp[i-1][j]; //exclude it
if(j >= a[i-1]) // if the number present at a[i-1] is less than or equal to the sum
dp[i][j] = (dp[i-1][j] || dp[i-1][j-a[i-1]]); //include OR exclude it
}
}
return dp[n][sum]; //returning there last cell of the dp matrix
}
int main()
{
int n;
cout<<"Enter the number of elements in the array :"<<endl;
cin>>n; // enter the number of elements in the array
int a[n];
cout<<"enter the elements"<<endl;
for(int i = 0; i < n; i++)
{
cin>>a[i]; //input the array elements
}
int sum;
cout<<"Enter the sum to be checked for: "<<endl;
cin>>sum;
if(checkSubsetSum(a,n,sum))
cout<<"Subset Found with sum: "<<sum; // if checkSubsetSum function is true
else
cout<<"No Subset found with sum: "<<sum; // if checkSubsetSum function is false
return 0;
}
//Time Complexity : O(sum*n)
//Space Complexity : O(sum*n)