-
Notifications
You must be signed in to change notification settings - Fork 0
/
DP-SubsetSum..cpp
85 lines (68 loc) · 1.84 KB
/
DP-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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include<bits/stdc++.h>
using namespace std;
// time and space complexity :O(n*sum)
// memoization
int dp[100][10001];
int solve(int n,int arr[],int sum,int idx){
if(sum==0){
return dp[idx][sum]=1;
}
if(idx>=n){
return 0;
}
if(dp[idx][sum]!=-1){
return dp[idx][sum];
}
if(arr[idx]>sum){
return dp[idx][sum]=solve(n,arr,sum,idx+1);
}
return dp[idx][sum]= (solve(n,arr,sum,idx+1) || solve(n,arr,sum-arr[idx],idx+1));
}
bool isSubsetSum(int N, int arr[], int sum){
// code here
memset(dp,-1,sizeof(dp));
return solve(N,arr,sum,0);
}
// tabulation
bool SubsetSum(int set[],int n,int sum){
bool subset[n+1][sum+1];
for(int i=0;i<=n;i++)
{
subset[i][0]=true;
}
for(int i=1;i<=sum;i++)
{
subset[0][i]=false;
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=sum;j++)
{
if(j<set[i-1])
subset[i][j]=subset[i-1][j];
if(j>=set[i-1])
{
subset[i][j]=subset[i-1][j] ||subset[i-1][j-set[i-1]];
}
}
}
for(int i=0;i<=n;i++)
{
for(int j=0;j<=sum;j++)
{
cout<<subset[i][j]<<" ";
}
cout<<endl;
}
return subset[n][sum];
}
int main(){
int set[]={ 3, 34, 4, 12, 5, 2 };
int sum=9;
int n=sizeof(set)/sizeof(set[0]);
if(SubsetSum(set,n,sum))
cout<<"found subset\n";
else
cout<<"subset not found\n";
return 0;
}