-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCount_subsets_given_sum.cpp
More file actions
38 lines (35 loc) · 879 Bytes
/
Count_subsets_given_sum.cpp
File metadata and controls
38 lines (35 loc) · 879 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
#include<iostream>
#include<vector>
using namespace std;
int sum_subsets(int x, int subset[], int size){
int dp[size+1][x+1];
for(int i = 0; i < x; i++ ){
dp[0][i] = 0;
}
for(int i = 0; i < size; i++ ){
dp[i][0] = 1;
}
for(int i = i; i < size; i++){
for(int j = 1; j < x; j++){
if(subset[i-1] > j){
dp[i][j] = dp[i-1][j];
}
else if(subset[i-1] <= j){
dp[i][j] = dp[i-1][j] + dp[i-1][j-subset[i-1]];
}
}
}
for(int i = i; i < size; i++){
for(int j = 1; j < x; j++){
cout << dp[i][j] << ' ';
}
cout << endl;
}
return dp[size][x];
}
int main(){
int subset[5] = {1,2,3,4,5};
int size = sizeof(subset)/sizeof(subset[1]);
int x = 10;
cout << sum_subsets(x,subset,size) << endl;
}