forked from coder2hacker/Explore-open-source
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCount of Subset Sum.cpp
More file actions
40 lines (37 loc) · 868 Bytes
/
Count of Subset Sum.cpp
File metadata and controls
40 lines (37 loc) · 868 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
40
#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define fast \
cin.sync_with_stdio(false); \
cin.tie(NULL); \
cout.tie(NULL);
int dp[1001][10001];
int CountOfSubsetSum(int n, int arr[], int sum)
{
for (int i = 0; i <= n; i++)
dp[i][0] = 1;
for (int i = 1; i <= sum; i++)
dp[0][i] = 0;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= sum; 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];
}
}
return dp[n][sum];
}
signed main()
{
fast;
int n = 4;
int arr[4] = {1, 2, 3, 3};
int sum = 6;
//memonization
memset(dp, -1, sizeof(dp));
cout << CountOfSubsetSum(n, arr, sum);
return 0;
}