-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsubset_sum.cpp
More file actions
35 lines (32 loc) · 749 Bytes
/
subset_sum.cpp
File metadata and controls
35 lines (32 loc) · 749 Bytes
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
// subset or subsequence with sum s possible or not
// dp - O(n*sum)
// recursive dp
const ll S=2e5+1;
const ll N=101;
int dp[N][S];
int subset_sum(vll &a,ll n,ll s)
{
if(s==0) return 1;
if(n<=0) return 0;
if(dp[n][s]!=-1) return dp[n][s];
if(s<a[n-1])
return dp[n][s]=subset_sum(a,n-1,s);
else
return dp[n][s]=subset_sum(a,n-1,s)||subset_sum(a,n-1,s-a[n-1]);
}
//iterative dp
bool subset_sum(vll &a,ll n,ll s)
{
bool dp[n+1][s+1];
rep(i,0,n+1) dp[i][0]=true;
rep(i,1,s+1) dp[0][i]=false;
rep(i,1,n+1)
{
rep(j,1,s+1)
{
if(j<a[i-1]) dp[i][j]=dp[i-1][j];
else dp[i][j]=dp[i-1][j] || dp[i-1][j-a[i-1]];
}
}
return dp[n][s];
}