Prefix sum based approach


#1
    public int solve(int[] A) {
        
        int oddSum[] = new int[A.length];
        int evenSum[] = new int[A.length];
        
        // populate odd and even sums
        for (int i = 0; i < A.length; i++) {
            if (i % 2 == 1) {
                   evenSum[i] = A[i];  
                oddSum[i] = oddSum[i - 1];
            }
            
            else {
                if (i == 0) {
                    oddSum[i] = A[i];
                }
                
                else {
                    oddSum[i] = oddSum[i - 1] + A[i];
                    evenSum[i] = evenSum[i - 1];
                }
            }
        }
        
        int count = 0;
        int maxOddSum = oddSum[oddSum.length - 1];
        int maxEvenSum = evenSum[evenSum.length - 1];
        

        for (int i = 0; i < A.length; i++) {
            if (i % 2 == 1) {
               int currentOdd = maxEvenSum - evenSum[i] + oddSum[i];
               int currentEven =  maxOddSum - oddSum[i] + evenSum[i] - A[i];

               if ( currentOdd == currentEven)
                    count++;
            }
            
            else {
                int currentEven = maxOddSum - oddSum[i] + evenSum[i];
               int currentOdd =  maxEvenSum - evenSum[i] + oddSum[i] - A[i];
               
               if ( currentOdd == currentEven)
                    count++;
            }
                
        }
        return count;
    }
}`