-
Notifications
You must be signed in to change notification settings - Fork 14
/
0-sum_subset.cpp
81 lines (75 loc) · 1.96 KB
/
0-sum_subset.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Copyright (c) 2013 Elements of Programming Interviews. All rights reserved.
#include <algorithm>
#include <cassert>
#include <iostream>
#include <numeric>
#include <random>
#include <vector>
using std::cout;
using std::default_random_engine;
using std::endl;
using std::random_device;
using std::uniform_int_distribution;
using std::vector;
// @include
vector<int> find_0_sum_subset(const vector<int> &A) {
vector<int> prefix_sum(A);
for (int i = 0; i < prefix_sum.size(); ++i) {
prefix_sum[i] += i > 0 ? prefix_sum[i - 1] : 0;
prefix_sum[i] %= A.size();
}
vector<int> table(A.size(), -1);
for (int i = 0; i < A.size(); ++i) {
if (prefix_sum[i] == 0) {
vector<int> ans(i + 1);
iota(ans.begin(), ans.end(), 0);
return ans;
} else if (table[prefix_sum[i]] != -1) {
vector<int> ans(i - table[prefix_sum[i]]);
iota(ans.begin(), ans.end(), table[prefix_sum[i]] + 1);
return ans;
}
table[prefix_sum[i]] = i;
}
// @exclude
return {}; // it should not happen
// @include
}
// @exclude
void check_ans(const vector<int> &A, const vector<int> &ans) {
int sum = 0;
for (const int &a : ans) {
sum = (sum + A[a]) % A.size();
}
assert(sum == 0);
}
int main(int argc, char *argv[]) {
default_random_engine gen((random_device())());
for (int times = 0; times < 1000; ++times) {
int n;
vector<int> A;
if (argc == 2) {
n = atoi(argv[1]);
A.resize(n);
uniform_int_distribution<int> dis(0, 9999);
for (int i = 0; i < n; ++i) {
A[i] = dis(gen);
}
} else if (argc > 2) {
for (int i = 1; i < argc; ++i) {
A.emplace_back(atoi(argv[i]));
}
} else {
uniform_int_distribution<int> n_dis(1, 100);
n = n_dis(gen);
A.resize(n);
uniform_int_distribution<int> dis(0, 9999);
for (int i = 0; i < n; ++i) {
A[i] = dis(gen);
}
}
vector<int> ans = find_0_sum_subset(A);
check_ans(A, ans);
}
return 0;
}