-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountOfSubsetSum.cpp
More file actions
64 lines (55 loc) · 1.14 KB
/
CountOfSubsetSum.cpp
File metadata and controls
64 lines (55 loc) · 1.14 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
#include<bits/stdc++.h>
using namespace std;
int countOfSubsetSum(int arr[], int n, int sum)
{
int dp[n+1][sum+1];
for(int i=0;i<n+1;i++)
{
for(int j=0;j<sum+1;j++)
{
if(i==0)
{
dp[i][j] = 0;
}
if(j==0)
{
dp[i][j] = 1;
}
}
}
for(int i=1;i<n+1;i++)
{
for(int j=1;j<sum+1;j++)
{
if(arr[i-1]<=j)
{
dp[i][j] = dp[i-1][j-arr[i-1]] + dp[i-1][j];
} else {
dp[i][j] = dp[i-1][j];
}
}
}
// for(int i=0;i<n+1;i++)
// {
// for(int j=0;j<sum+1;j++)
// {
// cout<<dp[i][j]<<" ";
// }
// cout<<endl;
// }
return dp[n][sum];
}
int main() {
int n = 6;
int arr[n] = {2, 3, 5, 6, 8, 10};
int sum = 10;
cout<<countOfSubsetSum(arr, n, sum)<<endl;
}
// 1 0 0 0 0 0 0 0 0 0 0
// 1 0 1 0 0 0 0 0 0 0 0
// 1 0 1 1 0 1 0 0 0 0 0
// 1 0 1 1 0 2 0 1 1 0 1
// 1 0 1 1 0 2 1 1 2 1 1
// 1 0 1 1 0 2 1 1 3 1 2
// 1 0 1 1 0 2 1 1 3 1 3
// 3