AmazonOA #18
Get Computational Equivalents
Estimated MediumJudge-ready
Topics (AI-labelled)Two PointersBinary Search
Given an array of process loads and a threshold m, count unordered pairs (i,j) of distinct indices whose loads differ by at most m in absolute value. Each pair is counted once; pairs with equal values but different indices are counted too.
Input
The first line contains n m (1≤n≤200000, 0≤m≤10⁸), and the second line contains n loads (1..10⁸). The answer may exceed the range of a 32-bit integer.
Output
Output a single integer, the answer required by the problem.
Example 1
Input:
4 3
7 10 13 11
Output:
4
Example 2
Input:
3 0
5 5 5
Output:
3
Example 3
Input:
1 0
1
Output:
0
Example notes
The valid load pairs in example 1 are (7,10), (10,13), (10,11), (13,11), 4 pairs in total.
Editorial
Loading solution…