C++ Solution using prefix and suffix Arrays


#1

int Solution::solve(vector &A) {

vector<pair<int, int>> left(A.size(), {0,0});
vector<pair<int,int>> right(A.size(), {0,0});
int odd = 0, even = 0;
for(int i=0;i<A.size();i++){
    left[i] = make_pair(even, odd);
    if(i % 2 == 0)
        even += A[i];
    else
        odd += A[i];
}

odd = 0, even =0;
for(int i=A.size()-1;i>=0;i--){
    right[i] = make_pair(even, odd);
    if(i % 2 == 0)
        even += A[i];
    else
        odd += A[i];
}

int count = 0;
for(int i=0;i<A.size();i++){
    if((left[i].first + right[i].second) == (left[i].second + right[i].first))
        count++;
}
return count;

}