B - Pairs with Difference K Editorial /

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 333 点

問題文

高橋君は N 個の整数からなる数列を持っています。数列の i 番目の要素は A_i です。

高橋君は、この数列から異なる 2 つの要素を選び、その差がちょうど K になるようなペアの数を数えたいと考えています。

N 個の要素の中から 2 つを選ぶ方法のうち、選んだ 2 つの要素の差の絶対値がちょうど K となるペアの数を求めてください。ただし、i 番目の要素と j 番目の要素のペアと、j 番目の要素と i 番目の要素のペアは同じペアとして数えます。

制約

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq K \leq 10^9
  • 1 \leq A_i \leq 10^9
  • 入力はすべて整数

入力

N K
A_1 A_2 \ldots A_N
  • 1 行目には、数列の要素数を表す N と、目標とする差を表す K が、スペース区切りで与えられる。
  • 2 行目には、数列の各要素 A_1, A_2, \ldots, A_N が、スペース区切りで与えられる。

出力

差の絶対値がちょうど K となるペアの数を 1 行で出力してください。


入力例 1

5 2
1 3 5 7 9

出力例 1

4

入力例 2

7 3
1 4 7 10 2 5 8

出力例 2

5

入力例 3

10 100
50 150 250 350 450 200 300 400 500 600

出力例 3

8

Score : 333 pts

Problem Statement

Takahashi has a sequence of N integers. The i-th element of the sequence is A_i.

Takahashi wants to count the number of pairs where he selects two different elements from this sequence such that their difference is exactly K.

Among all ways to select 2 elements from the N elements, find the number of pairs where the absolute difference of the two selected elements is exactly K. Note that a pair of the i-th element and the j-th element is counted as the same pair as the j-th element and the i-th element.

Constraints

  • 2 \leq N \leq 2 \times 10^5
  • 1 \leq K \leq 10^9
  • 1 \leq A_i \leq 10^9
  • All inputs are integers

Input

N K
A_1 A_2 \ldots A_N
  • The first line contains N, the number of elements in the sequence, and K, the target difference, separated by a space.
  • The second line contains each element of the sequence A_1, A_2, \ldots, A_N, separated by spaces.

Output

Print the number of pairs whose absolute difference is exactly K in one line.


Sample Input 1

5 2
1 3 5 7 9

Sample Output 1

4

Sample Input 2

7 3
1 4 7 10 2 5 8

Sample Output 2

5

Sample Input 3

10 100
50 150 250 350 450 200 300 400 500 600

Sample Output 3

8