Trying to brash up algorithm development, with this one:
You are given an array of integers a and an integer k. Your task is to calculate the number of ways to pick two different indices i < j, such that a[i] + a[j] is divisible by k.
Example
For a = [1, 2, 3, 4, 5] and k = 3, the output should be solution(a, k) = 4.
this is my java solution:
long solution(int[] a, int k) {
final BiPredicate<Integer, Integer> isDivisible = (x,y) -> (a[x] + a[y]) % k == 0;
long counter = 0;
for(int i = 0; i < a.length - 1; i++) {
for(int j = i + 1; j < a.length; j++ ){
if(isDivisible.test(i,j)) {
counter++;
}
}
}
return counter;
}
It passes all the tests expect the performance ones, there must be some more performat solution, any ideas ?