JAVA - Codility Equilibrium test 100% score

My solution:

class Solution { 
    public int solution(int[] B) {
  
        // write your code in Java SE 8
        long[] rightToLeft = new long[A.length];
        long[] leftToRight = new long[A.length];
        long[] A = new long[B.length];
        int result = -1;
     
        // Convert int array to long array
        int j = 0;
        for (int value : B) {
            A[j++] = (long)value; 
        }
        
        for(int i=0; i<rightToLeft.length; i++){
            if(i==0){
             rightToLeft[i] = A[i];
            } else {
             rightToLeft[i] = A[i] + rightToLeft[i-1];
            }
        }

        for(int i=leftToRight.length-1; i>=0; i--){
            if(i==leftToRight.length-1){
                leftToRight[i] = A[i];
            } else {
                leftToRight[i] = A[i] + leftToRight[i+1];
            }
        }
        
        for(int i=0; i<rightToLeft.length; i++){
            if(rightToLeft[i] == leftToRight[i]) {
                result = i;
                break;
            }
        }
        return result;
    }
}

Comments

Popular posts from this blog

Oracle - duplicate fields in RECORD,TABLE or argument list are not permitted

Oracle - ORA-29282: invalid file ID, ORA-06512: at "SYS.UTL_FILE"