Easy C++ solution using even and odd prefix sum


#1
int Solution::solve(vector<int> &A) {
    int n = A.size();
    vector<long long > odd (n+1,0);
    vector<long long > even (n+1,0);
    odd[n-1] = 0;
    even[n-1] = 0;
    for(long long i=n-2;i>=0;i--){
        if((i-1)%2==0){
            even[i+1] = even[i+2] + A[i+1];
            odd[i+1] = odd[i+2];
        }else{
            odd[i+1] = odd[i+2] + A[i+1];
            even[i+1] = even[i+2];
        }
    }
    even[0] = even[1]+A[0];
    odd[0] = odd[1];
    int ans=0;
    
    for(long long i=0;i<n;i++){
        long long e=even[0] - A[i] - even[i+1];
        long long o=odd[0] - A[i] - odd[i+1];
        if(i%2==0){
            o+=even[i];
            e+=odd[i];
        }else{
            o+=even[i];
            e+=odd[i];
        }
        // cout<< "("<< e <<" "<< o <<")";
        if(o == e)
            ans++;
        
    }
    return ans;
}