public class Main {
public static int countPairsBruteForce(int[] b, int k) {
int count = 0;
int n = b.length;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (b[i] - b[j] == k) {
count++;
}
}
}
return count;
}
public static void main
(String[] args
) { int[] b = {1, 5, 3, 4, 2};
int k = 2;
System.
out.
println("Count of pairs: " + countPairsBruteForce
(b, k
)); }
}
cHVibGljIGNsYXNzIE1haW4gewogICAgcHVibGljIHN0YXRpYyBpbnQgY291bnRQYWlyc0JydXRlRm9yY2UoaW50W10gYiwgaW50IGspIHsKICAgICAgICBpbnQgY291bnQgPSAwOwogICAgICAgIGludCBuID0gYi5sZW5ndGg7CiAgICAgICAgZm9yIChpbnQgaSA9IDA7IGkgPCBuOyBpKyspIHsKICAgICAgICAgICAgZm9yIChpbnQgaiA9IGkgKyAxOyBqIDwgbjsgaisrKSB7CiAgICAgICAgICAgICAgICBpZiAoYltpXSAtIGJbal0gPT0gaykgewogICAgICAgICAgICAgICAgICAgIGNvdW50Kys7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgcmV0dXJuIGNvdW50OwogICAgfQogCiAgICBwdWJsaWMgc3RhdGljIHZvaWQgbWFpbihTdHJpbmdbXSBhcmdzKSB7CiAgICAgICAgaW50W10gYiA9IHsxLCA1LCAzLCA0LCAyfTsKICAgICAgICBpbnQgayA9IDI7CiAgICAgICAgU3lzdGVtLm91dC5wcmludGxuKCJDb3VudCBvZiBwYWlyczogIiArIGNvdW50UGFpcnNCcnV0ZUZvcmNlKGIsIGspKTsKICAgIH0KfQog