Wednesday, August 2, 2017

Count Pair Difference

Given N integers, count the number of pairs of integers whose difference is K.

Input Format

The first line contains N and K.
The second line contains N numbers of the set. All the N numbers are unique.

Constraints

  • 2 <= N <= (10^5)
  • 0 < K < (10^9)
  • Each integer will be greater than 0 and at least K smaller than (2^31)-1.

Output Format

An integer that tells the number of pairs of integers whose difference is K .

Sample Input

5 2 
1 5 3 4 2 

Sample Output

3

Explanation

There are 3 pairs of integers in the set with a difference of 2.

Solution:

n,d=[int(i) for i in input().split()]
a = [int(i) for i in input().split()]
s = set(a)
c = 0
for i in a:
    if i+d in s:
        c+=1
print(c)


Share: