-
Notifications
You must be signed in to change notification settings - Fork 136
/
Copy pathEqualSubsetSumPartition.cpp
61 lines (52 loc) · 1.67 KB
/
EqualSubsetSumPartition.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
51
52
53
54
55
56
57
58
59
60
61
#include <bits/stdc++.h>
using namespace std;
class PartitionSet {
public:
bool canPartition(const vector<int> &num) {
int n = num.size();
// find the total sum
int sum = 0;
for (int i = 0; i < n; i++) {
sum += num[i];
}
// if 'sum' is a an odd number, we can't have two subsets with same total
if (sum % 2 != 0) {
return false;
}
// we are trying to find a subset of given numbers that has a total sum of ‘sum/2’.
sum /= 2;
vector<vector<bool>> dp(n, vector<bool>(sum + 1));
// populate the sum=0 column, as we can always have '0' sum without including any element
for (int i = 0; i < n; i++) {
dp[i][0] = true;
}
// with only one number, we can form a subset only when the required sum is
// equal to its value
for (int s = 1; s <= sum; s++) {
dp[0][s] = (num[0] == s ? true : false);
}
// process all subsets for all sums
for (int i = 1; i < n; i++) {
for (int s = 1; s <= sum; s++) {
// if we can get the sum 's' without the number at index 'i'
if (dp[i - 1][s]) {
dp[i][s] = dp[i - 1][s];
} else if (s >= num[i]) { // else if we can find a subset to get the remaining sum
dp[i][s] = dp[i - 1][s - num[i]];
}
}
}
// the bottom-right corner will have our answer.
return dp[n - 1][sum];
}
};
int main(int argc, char *argv[]) {
PartitionSet ps;
vector<int> num = {1, 2, 3, 4};
cout << ps.canPartition(num) << endl;
num = vector<int>{1, 1, 3, 4, 7};
cout << ps.canPartition(num) << endl;
num = vector<int>{2, 3, 4, 6};
cout << ps.canPartition(num) << endl;
}
// The Code is contributed by Ritika Rawat