diff --git a/Recursion DP/subsetSumProblem.cpp b/Recursion DP/subsetSumProblem.cpp new file mode 100644 index 0000000..1b72248 --- /dev/null +++ b/Recursion DP/subsetSumProblem.cpp @@ -0,0 +1,60 @@ +#include +using namespace std; +#define mod 1e9+7 +#define ll long long +#define mp make_pair +#define t() int test;cin>>test;while(test--) +#define setbits(x) __builtin_popcountll(x) +#define si set +#define ii pair +#define que_max priority_queue +#define que_min priority_queue > +#define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); +#define endl '\n' + +const int N = 2e5 + 5; +int a[N]; + +//calculate subset sum using dynamic programming +void subsetSum(){ + + int n,sum; + cin>>n>>sum; //input the length of array and sum + vector v(n); + + for(int i=0;i>v[i]; //input the array elements + + bool t[n+1][sum+1]; + + for(int i=0;i<=n;i++){ //initialisation of the 2D array + for(int j=0;j<=sum;j++){ + if(i == 0) t[i][j] = false; + if(j == 0) t[i][j] = true; + } + } + + for(int i=1;i<=n;i++){ + for(int j=1;j<=sum;j++){ + if(v[i-1] <= j) + t[i][j] = t[i-1][j-v[i-1]] || t[i-1][j]; + else + t[i][j] = t[i-1][j]; + } + } + cout<