O(n) time complexity & O(1) space complexity w. C++


#1

int Solution::solve(vector &A) {
long long sume=0, sumo=0, rune=0, runo=0;
int ans=0;

for(int i=0; i<A.size(); i++)
    if(i%2 == 0)
        sume += A[i];
    else
        sumo += A[i];
        
for(int i=0; i<A.size(); i++){
    if(i%2 == 0){
        if(rune + sumo - runo == runo + sume - rune -A[i])
            ans++;
        rune += A[i];
    }else{
        if(runo + sume - rune == rune + sumo - runo -A[i])
            ans++;
        runo += A[i];
    }
}

return ans;

}