Codility/Lesson 5/Prefix Sums/PassingCars/Java


Code

// you can also use imports, for example:
// import java.util.*;

// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");

class Solution {
    public int solution(int[] A) {
        // write your code in Java SE 8
        int answer = 0;
        int zeroCount = 0;

        for(int i=0; i<A.length; i++) {
            if(A[i] == 0) {
                zeroCount++;
            } else {
                answer += zeroCount;

                if(answer > 1_000_000_000) {
                    return -1;
                }
            }
        }

        return answer;
    }
}

Result Report

  • https://app.codility.com/demo/results/training77Z7AH-PA7/