-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path805.splitArraySameAverage.cpp
50 lines (49 loc) · 1.25 KB
/
805.splitArraySameAverage.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
#include <bits/stdc++.h>
using namespace std;
bool splitArraySameAverage(vector<int> &nums) {
int n = nums.size();
int m = n / 2;
if (n == 1) {
return false;
}
int sum = accumulate(nums.begin(), nums.end(), 0);
for (int &x : nums) {
x = x * n - sum;
}
unordered_set<int> left;
cout << (1 << m) << endl;
cout << "234234" << endl;
for (int i = 1; i < (1 << m); i++) {
int tot = 0;
for (int j = 0; j < m; j++) {
if (i & (1 << j)) {
// cout << (i & (1 << j)) << endl;
tot += nums[j];
}
}
if (tot == 0) {
return true;
}
left.emplace(tot);
}
cout << (1 << (n - m)) << endl;
cout << "234234" << endl;
int rsum = accumulate(nums.begin() + m, nums.end(), 0);
for (int i = 1; i < (1 << (n - m)); i++) {
int tot = 0;
for (int j = m; j < n; j++) {
if (i & (1 << (j - m))) {
tot += nums[j];
}
}
if (tot == 0 || (rsum != tot && left.count(-tot))) {
return true;
}
}
return false;
}
int main() {
vector<int> a{1, 2, 3, 4, 5, 6, 7, 8};
cout << splitArraySameAverage(a) << endl;
return 0;
}