-
Notifications
You must be signed in to change notification settings - Fork 54
/
Subset Sum Problem.cpp
50 lines (43 loc) · 1.01 KB
/
Subset Sum Problem.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
//{ Driver Code Starts
//Initial template for C++
#include<bits/stdc++.h>
using namespace std;
// } Driver Code Ends
//User function template for C++
class Solution{
public:
bool isSubsetSum(vector<int>arr, int sum){
// code here
bool dp[sum+1] = {0};
dp[0]=1;
// for(int i=0;i<=sum;i++) cout<<dp[i]<<" "; cout<<endl;
for(int i=0;i<arr.size();i++) {
for(int j=sum;j>=arr[i];j--) {
if(dp[j-arr[i]]) dp[j]=1;
}
// if(dp[sum]) return 1;
// for(int i=0;i<=sum;i++) cout<<dp[i]<<" "; cout<<endl;
}
return dp[sum];
}
};
//{ Driver Code Starts.
int main()
{
int t;
cin>>t;
while(t--)
{
int N, sum;
cin >> N;
vector<int> arr(N);
for(int i = 0; i < N; i++){
cin >> arr[i];
}
cin >> sum;
Solution ob;
cout << ob.isSubsetSum(arr, sum) << endl;
}
return 0;
}
// } Driver Code Ends