Given an array of integers and a target value, determine the number of pairs of array elements that have a difference equal to the target value.  

Example 

$k=1$ 

$arr=[1,2,3,4]$   

There are three values that differ by $k=1$: $2-1=2$, $3-2=1$, and $4-3=1$.  Return $3$.  

Function Description

Complete the pairs function below.   

pairs has the following parameter(s):

int k: an integer, the target difference
int arr[n]: an array of integers   

Returns   

int: the number of pairs that satisfy the criterion

Input Format

The first line contains two space-separated integers $n$ and $k$, the size of $arr$ and the target value. 

The second line contains $n$ space-separated integers of the array $arr$.

Constraints

$2 \leq n \leq 10^5$  
$0 < k < 10^9$  
$0<arr[i]<2^{31}-1$    
each integer $\boldsymbol{arr}[i]$ will be unique

Sample Input
STDIN       Function
-----       --------
5 2         arr[] size n = 5, k =2
1 5 3 4 2   arr = [1, 5, 3, 4, 2]
    
Sample Output
3

Explanation

There are 3 pairs of integers in the set with a difference of 2: [5,3], [4,2] and [3,1]. .

Difficulty: MEDIUM

Solutions:
def pairs(a, k):
	a.sort()
	left = 0
	right = 1
	answer = 0
	while right < len(a):
		val = a[right] - a[left]
		if val == k:
			answer += 1
			left += 1
			right += 1
		elif val < k:
			right += 1
		else:
			left += 1
			if left == right:
				right += 1
	return answer
a = input().strip()
a = list(map(int, a.split(' ')))
_a_size = a[0]
_k = a[1]
b = input().strip()
b = list(map(int, b.split(' ')))
print(pairs(b, _k))


def main():
	(N, K) = (int(x) for x in sys.stdin.readline().split())
	A = [int(x) for x in sys.stdin.readline().split()]
	setA = set(A)
	count = 0
	for x in A:
		if x - K in setA:
			count = count + 1
	print(count)
import sys
main()


def pairs(a, k):
	vals = {x: 1 for x in a}
	answer = 0
	for c in a:
		try:
			if vals[c - k]:
				answer += 1
		except:
			continue
	return answer
a = input().strip()
a = list(map(int, a.split(' ')))
_a_size = a[0]
_k = a[1]
b = input().strip()
b = list(map(int, b.split(' ')))
print(pairs(b, _k))


def pairs(a, k):
	acceptable_values_hash = {}
	answer = 0
	for i in a:
		acceptable_values_hash[i - k] = 1
	for i in a:
		try:
			if acceptable_values_hash[i]:
				answer += 1
		except KeyError:
			pass
	return answer
a = input().strip()
a = list(map(int, a.split(' ')))
_a_size = a[0]
_k = a[1]
b = input().strip()
b = list(map(int, b.split(' ')))
print(pairs(b, _k))


def pairs(a, k):
	answer = 0
	vals = {}
	for i in a:
		vals[i] = [i - k, i + k]
	for k in vals:
		for j in vals[k]:
			if j in vals:
				answer += 1
	return int(answer / 2)
a = input().strip()
a = list(map(int, a.split(' ')))
_a_size = a[0]
_k = a[1]
b = input().strip()
b = list(map(int, b.split(' ')))
print(pairs(b, _k))


(n, k) = (int(x) for x in input().split())
a = sorted((int(x) for x in input().split()))
assert n == len(a)
(c, i, j) = (0, 0, 1)
while j < n:
	if a[j] - a[i] < k:
		j += 1
	else:
		if a[j] - a[i] == k:
			c += 1
		i += 1
print(c)


(n, k) = map(int, input().split())
nums = set(map(int, input().split()))
nums_plus_k = set(map(lambda x: x + k, nums))
print(len(nums & nums_plus_k))


import sys
line1 = sys.stdin.readline().strip()
(n, k) = [int(x) for x in line1.split()]
numbers = [int(x) for x in sys.stdin.readline().strip().split()[:n]]
lookup_plus_k = set()
lookup_minus_k = set()
count = 0
for x in numbers:
	if x in lookup_plus_k:
		count += 1
	if x in lookup_minus_k:
		count += 1
	lookup_plus_k.add(x + k)
	lookup_minus_k.add(x - k)
sys.stdout.write(str(count))


(num_in, diff) = [int(item) for item in input().split()]
numbers = {int(item) for item in input().split()}
s = 0
for num in numbers:
	if num + diff in numbers:
		s += 1
print(s)


import sys
line = sys.stdin.readline()
nandk = [int(i) for i in line.split()]
while 1:
	n = int(nandk[0])
	k = int(nandk[1])
	line = sys.stdin.readline()
	alln = [int(i) for i in line.split()]
	alln.sort()
	pairs = 0
	i = 0
	j = 1
	while i < len(alln) and j < len(alln):
		diff = int(alln[j]) - int(alln[i])
		if diff == k and i != j:
			pairs += 1
			i += 1
			j += 1
		elif diff < k:
			j += 1
		else:
			i += 1
		if i == j:
			j = i + 1
	print(pairs)
	line = sys.stdin.readline()
	if not line:
		break
	nandk = [int(i) for i in line.split()]


import sys
(dim, k) = sys.stdin.readline().split()
values = set(sys.stdin.readline().split())
count = 0
for v in values:
	diff = int(v) - int(k)
	if str(diff) in values:
		count += 1
print(count)


line0 = input().split()
array = set(map(int, input().split()))
N = int(line0[0])
K = int(line0[1])
no = 0
for i in array:
	if i + K in array:
		no += 1
	if i - K in array:
		no += 1
print(no // 2)


line1 = input().split()
line2 = input().split()
(n, k) = (int(line1[0]), int(line1[1]))
array = [int(x) for x in line2]
diff = set()
visited = set()
count = 0
for i in range(n):
	diff.add(array[i])
for i in range(n):
	if array[i] + k in diff and (array[i], array[i] + k) not in visited:
		visited.add((array[i], array[i] + k))
		count += 1
	if array[i] - k in diff and (array[i] - k, array[i]) not in visited:
		visited.add((array[i] - k, array[i]))
		count += 1
print(count)


def count_kdistant_pairs(k, numbers):
	i = iter(numbers)
	j = iter(numbers)
	a = next(i)
	b = next(j)
	count = 0
	try:
		while True:
			if a + k == b:
				count += 1
				a = next(i)
				b = next(j)
			elif a + k < b:
				a = next(i)
			else:
				b = next(j)
	except StopIteration:
		pass
	return count
(n, k) = map(int, input().split())
numbers = sorted(map(int, input().split()))
print(count_kdistant_pairs(k, numbers))


first_line = input().split()
diff = int(first_line[1])
numbers = input().split()
dict = {}
num_pairs = 0
for item in numbers:
	item = int(item)
	dict[item] = 1
	if item + diff in dict.keys():
		num_pairs += 1
	if item - diff in dict.keys():
		num_pairs += 1
print(num_pairs)


a = input('')
a = a.split()
a = [int(x) for x in a]
data = input('')
data = data.split()
data = [int(x) for x in data]
data.sort()
n = a[0]
k = a[1]
t = 1
count = 0
for i in range(0, n):
	if t == n + 1:
		break
	for j in range(t, n):
		temp = data[j] - data[i]
		if temp == k:
			t = j + 1
			count = count + 1
			break
		elif temp > k:
			t = j
			break
print(count)


def pairs_num(m, ar):
	diff = m[-1]
	ar2 = [x + diff for x in ar]
	print(len(set(ar).intersection(set(ar2))))
m = [int(i) for i in input().strip().split()]
ar = [int(i) for i in input().strip().split()]
pairs_num(m, ar)


import sys
from bisect import bisect_left

def count_pairs(numbers, K):
	count = 0
	numbers = sorted(numbers)
	l = len(numbers)
	for (i, n) in enumerate(numbers):
		val = n + K
		ii = bisect_left(numbers, val, lo=i + 1)
		while ii < l and numbers[ii] == val:
			count += 1
			ii += 1
	return count
lines = sys.stdin.read()
(N, K, *numbers) = lines.split()
K = int(K)
numbers = list(map(int, numbers))
print(count_pairs(numbers, K))


(n, k) = [int(i) for i in input().strip().split()]
ar = [int(i) for i in input().strip().split()]
rt = 0
d = {}
for i in ar:
	d[i + k] = 1
for i in ar:
	if d.get(i, 0) != 0:
		rt += 1
print(rt)


def pair(N, K, List):
	times = 0
	for i in range(N):
		temp = List[i] + K
		j = i
		while j <= N - 1 and List[j] <= temp:
			if temp == List[j]:
				times = times + 1
			j = j + 1
	return times
aa = input()
a = aa.split()
N = int(a[0])
K = int(a[1])
b = input()
M = b.split()
L = []
for each in M:
	L.append(int(each))
L.sort()
result = pair(N, K, L)
print(result)


NK = input()
N = int(NK.partition(' ')[0])
K = int(NK.partition(' ')[2])
numbers = [int(x) for x in input().strip().split(' ')]
numbers.sort()
count = 0
for (index, num) in enumerate(numbers):
	while index < N and num + K > numbers[index]:
		index += 1
	if index == N:
		continue
	elif num + K == numbers[index]:
		count += 1
print(count)


n = input('')
m = input('')
arr = []
inp = []
arr = m.split()
inp = n.split()
arr1 = []
arr2 = []
for i in range(len(arr)):
	arr1.append(int(arr[i]))
k = int(inp[1])
for i in arr1:
	arr2.append(i + k)
print(len(list(set(arr1).intersection(arr2))))


x = input().split()
a = input().split()
N = int(x[0])
K = int(x[1])
d = {}
count = 0
for c in a:
	d[int(c)] = 1
for c in a:
	try:
		tmp = d[int(c) + K]
		count += 1
	except KeyError:
		pass
	finally:
		pass
print(count)


def search(ar, diff):
	count = 0
	for set_value in ar:
		if int(set_value) + int(diff) in ar:
			count += 1
	print(count)
m = input()
m_ar = m.split(' ')
diff = m_ar[1]
ar = [int(i) for i in input().strip().split()]
ar = set(ar)
search(ar, diff)


n = input().split()
nums = input().split()
for i in range(len(n)):
	n[i] = int(n[i])
for i in range(len(nums)):
	nums[i] = int(nums[i])
numSorted = sorted(nums)
k = n[1]
count = 0
for i in range(len(numSorted)):
	for j in range(i + 1, len(numSorted)):
		if numSorted[j] - numSorted[i] > k:
			break
		elif numSorted[j] - numSorted[i] == k:
			count += 1
			break
		else:
			pass
print(count)


def binSearch(elements, key, start, end):
	mid = (start + end) // 2
	if mid == end:
		if elements[mid] == key:
			return 1
		else:
			return 0
	else:
		if key == elements[mid]:
			return 1
		if key < elements[mid]:
			return binSearch(elements, key, start, mid - 1)
		else:
			return binSearch(elements, key, mid + 1, end)

def getCount(elements, diff):
	elements.sort()
	count = 0
	for element in elements:
		count = count + binSearch(elements, element + diff, 0, len(elements) - 1)
	return count
pos = [int(i) for i in input().strip().split()]
elements = [int(j) for j in input().strip().split()]
print(getCount(elements, pos[1]))


(N, K) = [int(x) for x in input().split()]
A = [int(x) for x in input().split()]
A.sort()
j = 0
answer = 0
for i in range(len(A)):
	while j < len(A) and A[j] < A[i] + K:
		j += 1
	if j < len(A) and A[j] == A[i] + K:
		answer += 1
print(answer)


(n, k) = [int(x) for x in input().split()]
s = set((int(x) for x in input().split()))
r = 0
for x in s:
	if x - k in s:
		r += 1
print(r)


NK = input()
N = int(NK.partition(' ')[0])
K = int(NK.partition(' ')[2])
numbers = [int(x) for x in input().strip().split(' ')]
numbers.sort()
count = 0
for (index, num) in enumerate(numbers):
	while index < N and num + K > numbers[index]:
		index += 1
	if index == N:
		continue
	elif num + K == numbers[index]:
		count += 1
print(count)


import bisect, sys
(N, K) = list(map(int, sys.stdin.readline().split()))
numbers = sorted(map(int, sys.stdin.readline().split()))
total = 0
for n in numbers:
	idx = bisect.bisect_left(numbers, n + K)
	if idx < N and numbers[idx] == n + K:
		total += 1
print(total)


def main():
	(N, K) = input().split()
	N = int(N)
	K = int(K)
	numbers = [int(n) for n in input().split()]
	numbers.sort(reverse=False)
	diff = []
	for i in range(len(numbers) - 1):
		diff.append(numbers[i + 1] - numbers[i])
	count = 0
	for k in range(len(diff)):
		sum = 0
		for n in range(k, len(diff)):
			sum += diff[n]
			if sum == K:
				count += 1
				break
			elif sum > K:
				break
	print(count)
main()


from collections import defaultdict
(junk, K) = (int(i) for i in input().split())
array = [int(i) for i in input().split()]
d = defaultdict(int)
for i in array:
	d[i] += 1
pairs = 0
for i in d:
	if i + K in d:
		pairs += d[i + K]
print(pairs)


def main():
	(n, k) = list(map(int, input().strip().split()))
	s = set()
	c = 0
	l = list(map(int, input().strip().split()))
	for x in l:
		s.add(x)
		if x + k in s:
			c += 1
		if x - k in s:
			c += 1
	print(c)
main()


import sys
(bounds, numbers) = sys.stdin.readlines()
targetDiff = int(bounds.split()[1])
numbers = list(map(int, numbers.split()))
sortedNumbers = sorted(numbers)
result = 0
upperAt = 1
lowerAt = 0
while upperAt < len(sortedNumbers):
	if upperAt == lowerAt:
		upperAt += 1
	else:
		diff = sortedNumbers[upperAt] - sortedNumbers[lowerAt]
		if diff == targetDiff:
			result += 1
			upperAt += 1
			lowerAt += 1
		elif diff < targetDiff:
			upperAt += 1
		else:
			lowerAt += 1
print(result)


(nums, diff) = input().split()
nums = int(nums)
diff = int(diff)
list_nums = [int(entry) for entry in input().split()]
count = 0
list_nums.sort()
for i in range(nums):
	j = i + 1
	while j < nums and list_nums[j] <= list_nums[i] + diff:
		if list_nums[j] == list_nums[i] + diff:
			count += 1
		j += 1
print(count)


(N, K) = map(int, input().split())
nums = map(int, input().split())
nums = dict(((x, x) for x in nums))
ans = 0
for each in nums:
	if each + K in nums:
		ans += 1
print(ans)


import sys
i = sys.stdin.readline()
(a, b) = i.split()
b = int(b)
line = sys.stdin.readline()
l = [int(i) for i in line.split()]
l = sorted(l)
count = 0

def binarysearch(l, x, lo, hi):
	if hi < lo:
		return False
	else:
		mid = (lo + hi) // 2
		if l[mid] == x:
			return True
		elif l[mid] > x:
			return binarysearch(l, x, lo, mid - 1)
		else:
			return binarysearch(l, x, mid + 1, hi)
for n in l:
	if binarysearch(l, n + b, 0, len(l) - 1):
		count += 1
print(count)


str = input().split()
N = int(str[0])
K = int(str[1])
nums = map(int, input().split())
rnum = {}
total = 0
for num in nums:
	if rnum.get(num) == None:
		if rnum.get(num + K) != None:
			rnum[num + K] = 2
		else:
			rnum[num + K] = 1
		if rnum.get(num - K) != None:
			rnum[num - K] = 2
		else:
			rnum[num - K] = 1
	elif rnum.get(num) == 2:
		total += 2
	elif rnum.get(num) == 1:
		total += 1
		if rnum.get(num + K) != None:
			rnum[num + K] = 2
		else:
			rnum[num + K] = 1
		if rnum.get(num - K) != None:
			rnum[num - K] = 2
		else:
			rnum[num - K] = 1
print(total)


import copy
import math

def printArrayAsString(arToPrint):
	arStringInt = []
	for i in arToPrint:
		arStringInt.append(str(i))
	print(' '.join(arStringInt))

def quickSort(arToQuickSort):
	if len(arToQuickSort) <= 1:
		return arToQuickSort
	arToProcessLeft = []
	arToProcessRight = []
	p = arToQuickSort[0]
	for index in range(1, len(arToQuickSort)):
		if arToQuickSort[index] >= p:
			arToProcessRight.append(arToQuickSort[index])
			continue
		if arToQuickSort[index] < p:
			arToProcessLeft.append(arToQuickSort[index])
			continue
	concatenatedList = quickSort(arToProcessLeft) + [p] + quickSort(arToProcessRight)
	return concatenatedList

def calculatePairDiff(arToProcess, k):
	num = 0
	for x in range(len(arToProcess)):
		for y in range(x, len(arToProcess)):
			if arToProcess[y] - arToProcess[x] < k:
				continue
			if arToProcess[y] - arToProcess[x] == k:
				num += 1
			if arToProcess[y] - arToProcess[x] > k:
				break
	return num

def main():
	strKN = input()
	arStrKN = strKN.split()
	n = int(arStrKN[0])
	k = int(arStrKN[1])
	strEntryInt = input()
	arStringValidArray = strEntryInt.split()
	arIntValidArray = []
	for val in arStringValidArray:
		arIntValidArray.append(int(val))
	arIntValidSorted = quickSort(arIntValidArray)
	returnValue = calculatePairDiff(arIntValidSorted, k)
	print(returnValue)
main()


import sys
lines = [line.replace('\n', '') for line in sys.stdin]
(N, K) = [int(x) for x in lines[0].split()]
numbers1 = set([int(x) for x in lines[1].split()])
numbers2 = set([int(x) - K for x in lines[1].split()])
print(len(numbers1 & numbers2))


NK = input()
N = int(NK.partition(' ')[0])
K = int(NK.partition(' ')[2])
numbers = [int(x) for x in input().strip().split(' ')]
numbers.sort()
count = 0
for (index, num) in enumerate(numbers):
	while index < N and num + K > numbers[index]:
		index += 1
	if index == N:
		continue
	elif num + K == numbers[index]:
		count += 1
print(count)


def Pair():
	(p, p1) = ([], [])
	NandK = input()
	listn = input()
	temp = NandK.split(' ')
	pair = temp[0]
	diff = temp[1]
	templist = listn.split(' ')
	for x in range(0, int(pair)):
		p.append(int(templist[x]))
		p1.append(int(templist[x]) + int(diff))
	print(len(set(p).intersection(set(p1))))
Pair()


(number, diff) = input().split()
number = int(number)
diff = int(diff)
d = input().split()
s = list(map(int, d))
s.sort()
count = 0
for i in range(number):
	for j in reversed(range(i)):
		if s[i] - s[j] == diff:
			count = count + 1
		if s[i] - s[j] > diff:
			break
print(count)


import sys

def pairs(array, size, difference):
	array_set = set(array)
	count = 0
	for value in array_set:
		if value + difference in array_set:
			count += 1
	return count
lines = sys.stdin.readlines()
first_line = lines[0].rstrip().split(' ')
size = int(first_line[0])
difference = int(first_line[1])
second_line = lines[1].rstrip().split(' ')
array = [int(x) for x in second_line]
print(pairs(array, size, difference))


def merge(left, right):
	result = []
	i = 0
	j = 0
	k = 0
	while i < len(left) and j < len(right):
		if left[i] <= right[j]:
			result.append(left[i])
			i += 1
		else:
			result.append(right[j])
			j += 1
		k += 1
	if i < len(left):
		result += left[i:]
	if j < len(right):
		result += right[j:]
	return result

def mergesort(lst):
	if len(lst) <= 1:
		return lst
	else:
		middle = int(len(lst) / 2)
		left = mergesort(lst[:middle])
		right = mergesort(lst[middle:])
		return merge(left, right)

def count(array, k):
	array1 = mergesort(array)
	t = len(array)
	counter = 0
	for i in range(t):
		for j in range(i + 1, t):
			if array1[j] - array1[i] > k:
				break
			if array1[j] - array1[i] == k:
				counter += 1
				break
	return counter
a = input()
num = [int(x) for x in a.split()]
n = num[0]
k = num[1]
string = input()
nums = [int(x) for x in string.split()]
print(count(nums, k))


import sys
(n, k) = map(int, sys.stdin.readline().strip().split())
all_numbers = map(int, sys.stdin.readline().strip().split())
s = set(all_numbers)
counter = 0
for number in s:
	if number - k in s:
		counter += 1
print(counter)


e = input().strip().split(' ')
N = int(e[0])
K = int(e[1])
Ns = set([int(x) for x in input().strip().split(' ')])
count = 0
for x in Ns:
	if x + K in Ns:
		count += 1
print(count)


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
b = {}
for i in a:
	if i in b:
		b[i] += 1
	else:
		b[i] = 1
ans = 0
for i in a:
	j = i + k
	if j in b:
		ans += b[j]
print(ans)


l = [int(i) for i in input().split()]
(n, k) = (l[0], l[1])
a = [int(i) for i in input().split()]
ct = 0
elems = set(a)
for i in elems:
	res = abs(i - k)
	if res in elems and res != i:
		ct += 1
print(ct)


NK = input()
N = int(NK.partition(' ')[0])
K = int(NK.partition(' ')[2])
numbers = [int(x) for x in input().strip().split(' ')]
numbers.sort()
count = 0
for (index, num) in enumerate(numbers):
	while index < N and num + K > numbers[index]:
		index += 1
	if index == N:
		continue
	elif num + K == numbers[index]:
		count += 1
print(count)


params = input().split()
int_list = input()
test_numbers = [int(x) for x in int_list.split()]
solution_numbers = [int(x) + int(params[1]) for x in int_list.split()]
if len(test_numbers) == int(params[0]):
	matching = set(test_numbers).intersection(set(solution_numbers))
	print(len(matching))


in1 = input().split()
a = input().split()
b = []
diff = int(in1[1], 10)
N = int(in1[0], 10)
for i in range(N):
	b.append(str(diff + int(a[i], 10)))
print(len(set(a) & set(b)))


import sys
nk = sys.stdin.readline().split()
(n, k) = map(int, nk)
numbers = sys.stdin.readline().split()
numset = {int(x) for x in numbers}
kset = {x + k for x in numset}
valid_pairs = numset.intersection(kset)
print(len(valid_pairs))


k = int(input().split()[1])
nums = list(map(int, input().split()))
seeking = set()
twice = set()
found = 0
for num in nums:
	if num in seeking:
		found += 1
		if num in twice:
			found += 1
	for x in [num - k, num + k]:
		(twice if x in seeking else seeking).add(x)
print(found)


(n, k) = input().split()
n = int(n)
k = int(k)
l = list(map(int, input().split()))
s = set()
for e in l:
	s.add(e)
count = 0
for e in s:
	if e - k in s:
		count = count + 1
print(count)


(n, k) = map(int, input().split())
nums = set([int(num) for num in input().split()])
pairings = 0
targets = set([int(num) + k for num in nums])
print(len(targets & nums))


from collections import deque

def main():
	string = input()
	n = string.split()[0]
	k = int(string.split()[1])
	count = 0
	numbers = input()
	numbers = numbers.split()
	q = []
	for i in numbers:
		i = int(i)
		q.append(i)
	q.sort()
	r = []
	for i in q:
		r.append(i - k)
	r.pop(0)
	for j in r:
		while len(q) > 0:
			if j > q[0]:
				q.pop(0)
			elif j == q[0]:
				count += 1
				break
			else:
				break
	print(count)
main()


NK = input()
N = int(NK.partition(' ')[0])
K = int(NK.partition(' ')[2])
numbers = [int(x) for x in input().strip().split(' ')]
numbers.sort()
count = 0
for (index, num) in enumerate(numbers):
	while index < N and num + K > numbers[index]:
		index += 1
	if index == N:
		continue
	elif num + K == numbers[index]:
		count += 1
print(count)


def count1(A, low, high, k):
	d = A[high] - A[low]
	if d == k:
		c = 1
	elif d > k:
		c = 0
	else:
		return 0
	c1 = count(A, low, high - 1, k)
	c2 = count(A, low + 1, high, k)
	return c1 + c2 + c

def count2(A, low, high, k):
	c = 0
	l = low
	h = low + 1
	while h <= high:
		d = A[h] - A[l]
		if d < k:
			h = h + 1
		elif d > k:
			l = l + 1
			if l == h:
				h = h + 1
		else:
			c = c + 1
			l = l + 1
			h = h + 1
	return c

def count(A, low, high, k):
	c = 0
	for i in range(1, len(A)):
		for j in range(0, i):
			if abs(A[i] - A[j]) == k:
				c = c + 1
	return c
w = [int(x) for x in input().split()]
n = w[0]
k = w[1]
A = [int(x) for x in input().split()]
A.sort()
print(count2(A, 0, len(A) - 1, k))


def run():
	(N, K) = [int(i) for i in input().strip().split(' ')]
	numbers = set([int(i) for i in input().strip().split(' ')])
	count = 0
	for num in numbers:
		(x, y) = (num + K, num - K)
		if x in numbers:
			count += 1
		if y in numbers:
			count += 1
	print(int(count / 2))
run()


(n, k) = input().split()
n = int(n)
k = int(k)
numbers = input().split()
numbers = [int(x) for x in numbers]
numbers.sort()
left = 0
right = 0
res = 0
while left < n and right < n:
	if numbers[right] - numbers[left] > k:
		left += 1
	elif numbers[right] - numbers[left] < k:
		right += 1
	else:
		left_tmp = left
		while left_tmp < n and numbers[left_tmp] == numbers[left]:
			left_tmp += 1
		right_tmp = right
		while right_tmp < n and numbers[right_tmp] == numbers[right]:
			right_tmp += 1
		res += (left_tmp - left) * (right_tmp - right)
		left = left_tmp
print(res)


import sys
N = None
K = None
mod_list = []
for line in sys.stdin:
	if N is None:
		(N, K) = map(int, line.strip().split())
		for i in range(N):
			mod_list.append([])
		continue
	for val in map(int, line.strip().split()):
		mod_list[val % K].append(val)
count = 0
for val_list in mod_list:
	if len(val_list) <= 1:
		continue
	val_list.sort()
	prev_val = None
	for val in val_list:
		if prev_val is not None and val - prev_val == K:
			count += 1
		prev_val = val
sys.stdout.write('%d' % count)


ar1 = input().split()
ar1 = list(map(int, ar1))
ar2 = input().split()
ar2 = list(map(int, ar2))
ar3 = [x + int(ar1[1]) for x in ar2]
ar4 = list(set(ar2) & set(ar3))
print(len(ar4))


import sys
from collections import defaultdict

def main():
	(n, k) = [int(x) for x in sys.stdin.readline().split()]
	data = [int(x) for x in sys.stdin.readline().split()]
	d = defaultdict(int)
	for x in data:
		d[x] += 1
	res = 0
	for x in data:
		res += d[x + k]
	print(res)
main()


(N, K) = input().split()
numbers = {int(i) for i in input().split()}
count = len([i for i in numbers if i + int(K) in numbers])
print(count)


def bsearch(s, e, first, last, calls):
	if last - first < 2:
		return s[first] == e or s[last] == e
	mid = first + (last - first) // 2
	if s[mid] == e:
		return True
	if s[mid] > e:
		return bsearch(s, e, first, mid - 1, calls + 1)
	return bsearch(s, e, mid + 1, last, calls + 1)

def pairs(a, k):
	answer = 0
	a = sorted(a)
	for (i, x) in enumerate(a):
		if bsearch(a, x + k, i, len(a) - 1, 1):
			answer += 1
	return answer
a = input().strip()
a = list(map(int, a.split(' ')))
_a_size = a[0]
_k = a[1]
b = input().strip()
b = list(map(int, b.split(' ')))
print(pairs(b, _k))


def pairs(a, k):
	h = {}
	answer = 0
	for n in a:
		h[n] = True
	for n in a:
		if n + k in h:
			answer += 1
	return answer
a = input().strip()
a = list(map(int, a.split(' ')))
_a_size = a[0]
_k = a[1]
b = input().strip()
b = list(map(int, b.split(' ')))
print(pairs(b, _k))


def pairs(a, k):
	a.sort()
	answer = 0
	prev_number = -1
	high_index = 0
	high_number_count = 0
	for i in range(len(a)):
		if a[i] == prev_number:
			answer += high_number_count
		else:
			if high_index == len(a):
				break
			high_number = a[i] + k
			prev_number = a[i]
			high_number_count = 0
			while high_index < len(a):
				if a[high_index] == high_number:
					answer += 1
					high_number_count += 1
				elif a[high_index] > high_number:
					break
				high_index += 1
	return answer
a = input().strip()
a = list(map(int, a.split(' ')))
_a_size = a[0]
_k = a[1]
b = input().strip()
b = list(map(int, b.split(' ')))
print(pairs(b, _k))


def pairs(a, k):
	a.sort()
	i = 0
	j = 1
	answer = 0
	while j < len(a):
		if a[j] - a[i] == k:
			answer += 1
			j += 1
		elif a[j] - a[i] > k:
			i += 1
		else:
			j += 1
	return answer
a = input().strip()
a = list(map(int, a.split(' ')))
_a_size = a[0]
_k = a[1]
b = input().strip()
b = list(map(int, b.split(' ')))
print(pairs(b, _k))


def pairs(a, k):
	dict = {}
	count = 0
	for temp in a:
		dict[temp] = 1
	for temp in a:
		target = temp - k
		if target in dict:
			count += 1
		target = temp + k
		if target in dict:
			count += 1
	return int(count / 2)
a = input().strip()
a = list(map(int, a.split(' ')))
_a_size = a[0]
_k = a[1]
b = input().strip()
b = list(map(int, b.split(' ')))
print(pairs(b, _k))


def pairs(a, k):
	save_dict = {}
	answer = 0
	for i in range(len(a)):
		if a[i] in save_dict:
			save_dict[a[i]] += 1
		else:
			save_dict[a[i]] = 1
	keys = save_dict.keys()
	for key in keys:
		if key + k in save_dict:
			answer += save_dict[key + k]
	return answer
a = input().strip()
a = list(map(int, a.split(' ')))
_a_size = a[0]
_k = a[1]
b = input().strip()
b = list(map(int, b.split(' ')))
print(pairs(b, _k))


def pairs(a, k):
	answer = 0
	vals = set(a)
	for i in range(len(a)):
		if a[i] - k in vals:
			answer += 1
		if a[i] + k in vals:
			answer += 1
	return int(answer / 2)
a = input().strip()
a = list(map(int, a.split(' ')))
_a_size = a[0]
_k = a[1]
b = input().strip()
b = list(map(int, b.split(' ')))
print(pairs(b, _k))


def pairs(a, k):
	s = set(a)
	answer = sum((1 for x in s if x + k in s))
	return answer
a = input().strip()
a = list(map(int, a.split(' ')))
_a_size = a[0]
_k = a[1]
b = input().strip()
b = list(map(int, b.split(' ')))
print(pairs(b, _k))


def pairs(a, k):
	answer = 0
	seen = set()
	for x in a:
		answer += len([y for y in [x - k, x + k] if y in seen])
		seen.add(x)
	return answer
a = input().strip()
a = list(map(int, a.split(' ')))
_a_size = a[0]
_k = a[1]
b = input().strip()
b = list(map(int, b.split(' ')))
print(pairs(b, _k))


def pairs(A, k):
	count = 0
	for i in A:
		temp1 = i + k
		temp2 = i - k
		if temp1 in A:
			count += 1
		if temp2 in A:
			count += 1
	return int(count / 2)
(N, K) = map(int, input().split())
a = list(map(int, input().split()))
A = {i: 1 for i in a}
count = pairs(A, K)
print(count)


def pairs(a, k):
	vals = {}
	for i in a:
		if i - k in vals:
			vals[i - k] += 1
		else:
			vals[i - k] = 1
		if i + k in vals:
			vals[i + k] += 1
		else:
			vals[i + k] = 1
	numPairs = 0
	for i in a:
		if i in vals:
			numPairs += vals[i]
	return int(numPairs / 2)
a = input().strip()
a = list(map(int, a.split(' ')))
_a_size = a[0]
_k = a[1]
b = input().strip()
b = list(map(int, b.split(' ')))
print(pairs(b, _k))