Ctrl K

Pairs with an Exact Gap

Easy
You are given an integer array values and an integer gap. Return the number of index pairs i < j such that |values[i] - values[j]| = gap.
Example 1
values=[4, 7, 1, 4, 10], gap=3 → 5

A difference of 3 appears for the value pairs (4, 7) twice, since there are two 4s, (4, 1) twice as well, and (7, 10). That is 5 pairs.

Example 2
values=[2, 2, 2], gap=0 → 3

Any two elements are equal: pairs (0, 1), (0, 2), (1, 2), so 3.

Example 3
values=[5, 9], gap=2 → 0

The only pair differs by 4, not 2.

Constraints
  • 1 ≤ values.length ≤ 200
  • 1 ≤ values[i] ≤ 10⁵
  • 0 ≤ gap ≤ 10⁵
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsThe Candidate With Most Votes EasyValues One Apart EasyEven Piles of Cards EasyMatching Domino Tiles EasyWords From Letter Tiles Easy
loading Python…
Input
values=[4, 7, 1, 4, 10], gap=3
Expected
5

Run checks the first 3 tests in your browser. Submit checks all 108 tests on the server, hidden ones included.