-
Notifications
You must be signed in to change notification settings - Fork 45
/
3partition.cpp
62 lines (46 loc) · 1.11 KB
/
3partition.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
62
#include <iostream>
#include <vector>
#include <numeric>
using namespace std;
bool subsetSum(vector<int> const &S, int n, int a, int b, int c)
{
if (a == 0 && b == 0 && c == 0) {
return true;
}
if (n < 0) {
return false;
}
bool A = false;
if (a - S[n] >= 0) {
A = subsetSum(S, n - 1, a - S[n], b, c);
}
bool B = false;
if (!A && (b - S[n] >= 0)) {
B = subsetSum(S, n - 1, a, b - S[n], c);
}
bool C = false;
if ((!A && !B) && (c - S[n] >= 0)) {
C = subsetSum(S, n - 1, a, b, c - S[n]);
}
return A || B || C;
}
bool partition(vector<int> const &S)
{
int n = S.size();
if (n < 3) {
return false;
}
int sum = accumulate(S.begin(), S.end(), 0);
return !(sum % 3) && subsetSum(S, n - 1, sum/3, sum/3, sum/3);
}
int main()
{
vector<int> S = { 7, 3, 2, 1, 5, 4, 8 };
if (partition(S)) {
cout << "Set can be partitioned";
}
else {
cout << "Set cannot be partitioned";
}
return 0;
}