-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubset _sum.cpp
84 lines (69 loc) · 1.16 KB
/
subset _sum.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
#include<bits/stdc++.h>
using namespace std;
//recursive approach
//complexity is O(2^n)
/*bool subsetsum(int arr[],int n,int k){
if(k==0){
return true;
}
if(n==0 && k!=0){
return false;
}
if(k>0 && n>0){
return subsetsum(arr,n-1,k-arr[n]) || subsetsum(arr,n-1,k);
}
return 0;
}*/
int main(){
int n;
cin>>n;
int a[n];
for(int i=0;i<n;i++){
cin>>a[i];
}
int key=0;cin>>key;
/*bool t=subsetsum(arr,n,key);
if(t){
cout<<"yes";
}
else{
cout<<"no";
}*/
bool dp[n+1][key+1];
for(int i=0;i<=n;i++){
dp[i][0]=true;
}
for(int i=0;i<=n;i++){
for(int j=1;j<=key;j++){
if(a[i]>key){
if(i==0){
dp[i][j]=false;
}
else{
dp[i][j]=dp[i-1][j];}
}
if(a[i]<key){
if(i==0){
if(a[i]==key){
dp[i][j]=true;
}
else{
dp[i][j]=false;
}
}
else{
dp[i][j]=dp[i-1][j] or dp[i-1][j-a[i]];}
}
}
}
for(int i=0;i<=n;i++){
for(int j=0;j<=key;j++){
cout<<dp[i][j]<<" ";}cout<<endl;}
if(dp[n][key]){
cout<<"yes";
}
else{
cout<<"no";
}
system("pause");
}