-
Notifications
You must be signed in to change notification settings - Fork 77
/
subset_sum.cpp
35 lines (30 loc) · 1.03 KB
/
subset_sum.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
// Returns true if there is a subset of set[] with sun equal to given sum
bool isSubsetSum(vector<int>& set, int sum)
{
// The value of subset[i][j] will be true if there is a subset of set[0..j-1]
// with sum equal to i
int n = (int)set.size();
vector<vector<bool>> dp(sum + 1, vector<bool>(n));
// If sum is 0, then answer is true
for (int i = 0; i < n; i++) dp[0][i] = true;
// If sum is not 0 and set is empty, then answer is false
for (int i = 1; i <= sum; i++) dp[i][0] = false;
// Fill the subset table in botton up manner
for (int i = 1; i <= sum; i++) {
for (int j = 0; j < n; j++) {
dp[i][j] = dp[i][j - 1];
if (i - set[j] >= 0) {
dp[i][j] = dp[i][j] | dp[i - set[j]][j - 1];
}
}
}
/*
// uncomment this code to print table
for (int i = 0; i <= sum; i++) {
for (int j = 0; j <= n; j++)
printf ("%4d", subset[i][j]);
printf("\n");
}
*/
return dp[sum][n];
}