-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDp_4.cpp
More file actions
39 lines (28 loc) · 916 Bytes
/
Dp_4.cpp
File metadata and controls
39 lines (28 loc) · 916 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
36
37
38
39
#include <bits/stdc++.h>
using namespace std;
int findWaysUtil(int ind, int target, vector<int>& arr, vector<vector<int>> &dp){
if(ind == 0){
if(target==0 && arr[0]==0)
return 2;
if(target==0 || target == arr[0])
return 1;
return 0;
}
if(dp[ind][target]!=-1)
return dp[ind][target];
int notTaken = findWaysUtil(ind-1,target,arr,dp);
int taken = 0;
if(arr[ind]<=target)
taken = findWaysUtil(ind-1,target-arr[ind],arr,dp);
return dp[ind][target]= notTaken + taken;
}
int findWays(vector<int> &num, int k){
int n = num.size();
vector<vector<int>> dp(n,vector<int>(k+1,-1));
return findWaysUtil(n-1,k,num,dp);
}
int main() {
vector<int> arr = {0,0,1};
int k=1;
cout<<"The number of subsets found are " <<findWays(arr,k);
}