-
Notifications
You must be signed in to change notification settings - Fork 18
/
code_BU(Two Sets II).cpp
39 lines (30 loc) · 1.22 KB
/
code_BU(Two Sets II).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
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
int n,mod=1e9+7;
cin>>n;
int S = n*(n+1)/2; //calculate total sum 1 to n
int s = S/2; // total sum / 2 = requires sum of each subset
vector<vector<int>> dp(n+1,vector<int>(s+1,0)); //2D vector declared
//dp[i][sum] = no. of ways to make sum using numbers 0 to i
dp[0][0] = 1;// number of ways to make 0 using 0 = 1
for(int sum=0;sum<=s;sum++)
{
for(int i=1;i<n;i++)//till n-1 as we permanently put n into a particular subset to avoid repeats
{
dp[i][sum] = dp[i-1][sum];
if(sum - i >= 0)
{
dp[i][sum] = dp[i-1][sum] + dp[i-1][sum-i];
// ways = (either not include that number keeping the sum same) or (include the number decreasing sum by i.)
dp[i][sum] %= mod;
}
}
}
(S%2 != 0) ? (cout<<0<<endl) : (cout<<dp[n-1][s]<<endl); //if S is odd, it is not possible to divide it into 2 equal subsets, so print 0.
return 0;
}