110. Pairs With a Given Difference
Challenge1000 ms256 MBSolved by 0%
You are given n distinct integers and a value k ≥ 0.
Print how many pairs of positions (i, j) with i < j satisfy
|a[i] − a[j]| = k.
Constraints - `1 ≤ n ≤ 200000` - `0 ≤ k ≤ 1000000000` - `-1000000000 ≤ a[i] ≤ 1000000000` - All values are distinct.
Input
The first line contains two integers n and k.
The second line contains n space-separated integers.
Output
Print one integer, how many pairs of positions differ by exactly k.
Input5 2
1 5 3 4 2
Output3
Notehas three pairs two apart
Input4 0
1 2 3 4
Output0
Notehas a difference of zero and all values distinct, so no pair qualifies
Hints
Four rungs, in order. The last two open once you have submitted an attempt. A wrong one counts.
Hint 1Where to start
Hint 2The approachOpen hint 1 first. This one carries on from it.
Hint 3PseudocodeOpen hint 2 first. This one carries on from it.
Hint 4Full solutionOpen hint 3 first. This one carries on from it.