Answers for "balance array interviewbit"

1

balance array interviewbit

public class Solution {
    
    public int solve(ArrayList<Integer> A) {
        
        int n = A.size();
        int result = 0;
        int even = 0;
        int odd =0;
        
        for(int i = 0 ; i < A.size() ; i++){
            if(i%2 == 0)
                even += A.get(i);
            else
                odd += A.get(i);
        }
        
        int keven = 0;
        int kodd =0;
        
        for(int i = 0 ; i < A.size() ; i++){
            if(i%2 == 0){
                if((even - (A.get(i)+keven) + kodd) == ((odd-kodd) + keven))
                    result += 1;
                keven += A.get(i);    
            }
            else{
                if((odd - (A.get(i)+kodd) + keven) == (kodd + (even-keven)))
                    result += 1;
                kodd += A.get(i);    
            }
        }
        
        return result;
        
    }
}
Posted by: Guest on August-07-2020

Code answers related to "balance array interviewbit"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language