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 M, int[] A) {
// write your code in Java SE 8
int answer = 0;
boolean checked[] = new boolean[M+1];
int right = 0;
for(int left = 0; left<A.length; left++) {
while(right < A.length && !checked[A[right]]) {
answer += (right - left + 1);
checked[A[right]] = true;
right++;
if(answer >= 1000000000) {
return 1000000000;
}
}
checked[A[left]] = false;
if(answer >= 1000000000) {
return 1000000000;
}
}
return answer;
}
}
Result URL
https://app.codility.com/demo/results/trainingQ6GPXQ-7JV/