-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBalance_array.cpp
More file actions
33 lines (33 loc) · 811 Bytes
/
Copy pathBalance_array.cpp
File metadata and controls
33 lines (33 loc) · 811 Bytes
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
int Solution::solve(vector<int> &A) {
int n = A.size();
vector<long long int> os(n+1, 0), es(n+1, 0);
// Fisrt is odd || second is even
for(int i=1; i<=n; i++){
if((i%2) == 0){
os[i] = os[i-1];
es[i] = es[i-1] + A[i-1];
}
else{
os[i] = os[i-1] + A[i-1];
es[i] = es[i-1];
}
}
int ans = 0;
for(int i=1; i<=n ; i++){
auto o1 = os[i-1];
auto e1 = es[i-1];
auto o2 = os[n] - os[i-1];
auto e2 = es[n] - es[i-1];
if((i%2) != 0){
o2 -= A[i-1];
}
else{
e2 -= A[i-1];
}
// printf("%d : (%d, %d) || (%d, %d) \n", i, o1, e2, e1, o2);
if((o1+e2) == (e1+o2)){
ans++;
}
}
return ans;
}