JS solution O(n) time, O(1) space


#1

JS solution O(n) time, O(1) space
`
solve : function(A){
let count = 0;

    let psEven = 0, psOdd = 0, sufEven = 0, sufOdd = 0;

    for(let i = 0; i < A.length; i++) {
        if (i % 2 === 0) {
            sufEven += A[i];
        } else {
            sufOdd += A[i];
        }
    }
        
    for(let i = 0; i < A.length; i++){

        if (i % 2 === 0) {
            sufEven -= A[i];
        } else {
            sufOdd -= A[i];
        }
        
        if (psEven + sufOdd === psOdd + sufEven) {
            count++;
        }
      
        if (i % 2 === 0) {
            psEven += A[i];
        } else {
            psOdd += A[i];
        }
    }
    return count;
}

`


#2

Hi Ruth, I tested this solution. It looks correct. I wanted to understand how did you arrive at this solution. What was the thought process.