Simple solution by using prefix sums


#1

int Solution::solve(vector &A) {

vector<long> preOdd , preEven ;

preOdd.push_back(0) , preEven.push_back(0) ;

for(int i = 0 ; i < A.size() ; i++){
    
    if(!(i & 1)){
        
        preOdd.push_back(preOdd.back() + A[i]) ;
        preEven.push_back(preEven.back()) ;
        
    }else{
        
        preEven.push_back(preEven.back() + A[i]) ;
        preOdd.push_back(preOdd.back()) ;
        
    }
}

int cnt = 0 ;

for(int i = 1 ; i <= A.size() ; i++){
    
    long evenSum = 0 , oddSum = 0 ;
    
    if(i & 1){
        
        evenSum = preEven[i] + preOdd[A.size()] - preOdd[i] ;
        oddSum = preOdd[i - 1] + preEven[A.size()] - preEven[i] ;
        
    }else{
        
        evenSum = preEven[i - 1] + preOdd[A.size()] - preOdd[i] ;
        oddSum = preOdd[i] + preEven[A.size()] - preEven[i];
        
    }
    
    if(evenSum == oddSum)
        cnt++ ;
}

return cnt ;

}