-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbalarray.cpp
More file actions
58 lines (49 loc) · 1.3 KB
/
balarray.cpp
File metadata and controls
58 lines (49 loc) · 1.3 KB
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
// Given an integer array A of size N. You need to count the number of special elements in the given array.
// A element is special if removal of that element make the array balanced.
// Array will be balanced if sum of even index element equal to sum of odd index element.
int Solution::solve(vector<int> &A) {
int n=A.size();
int od[n]={0}; int ev[n]={0};
od[0]=A[0]; if(n>=2) od[1]=od[0];
for(int i=2;i<n;i=i+2)
{
od[i]=od[i-2]+A[i];
if(i+1<n)
od[i+1]=od[i];
}
ev[0]=0;
if(n>=2) ev[1]=A[1];
if(n>=3) ev[2]=ev[1];
for(int i=3;i<n;i=i+2)
{
ev[i]=ev[i-2]+A[i];
if(i+1<n)
ev[i+1]=ev[i];
}
// for(int i=0;i<n;i++)
// cout<<od[i]<<" ";
// cout<<endl;
// for(int i=0;i<n;i++)
// cout<<ev[i]<<" ";
// cout<<endl;
int cp=0;
for(int i=0;i<n;i++)
{
int odd=0,even=0;
if(i%2==0)
{
odd=od[i]-A[i];
odd+=ev[n-1]-ev[i];
even=ev[i]+od[n-1]-od[i];
}
else
{
even=ev[i]-A[i];
even+=od[n-1]-od[i];
odd=od[i]+ev[n-1]-ev[i];
}
if(odd==even)
cp++;
}
return cp;
}