#323 Counting Pairs with a Given Difference

Companies:
Yandex
Themes:
ArraysPractice

An array of integers a_1, a_2, ..., a_n and a non-negative number k are given. Find the number of pairs of array elements (i, j) such that i < j and |a_i - a_j| = k.

Java
// Examples:
// {-5, 1, -3, 0, 5}, k = 6 -> 3
// {-2, 1, 3, 0, -10, 1}, k = 0 -> 1

public int getPairsCountWithDiffByModule(int[] a, int k) {
    ...
}

Question review

Mentor
Answer as you would in a real interview. I’ll ask follow-up questions, then compare your answer with the reference answer and show your accuracy as a %.
Sign in to start the dialogue The mentor keeps the context of the current question and your previous messages.
Sign in