[Chopsticks (singular: chopstick) are short, frequently tapered sticks used in pairs of equal length, which are used as the traditional eating utensils of China, Japan, Korea and Vietnam. Originated in ancient China, they can also be found in some areas of Tibet and Nepal that are close to Han Chinese populations, as well as areas of Thailand, Laos and Burma which have significant Chinese populations. Chopsticks are most commonly made of wood, bamboo or plastic, but in China, most are made out of bamboo. Chopsticks are held in the dominant hand, between the thumb and fingers, and used to pick up pieces of food.]
Retrieved from wikipedia
Actually, the two sticks in a pair of chopsticks need not be of the same length. A pair of sticks can be used to eat as long as the difference in their length is at most D. The Chef has N sticks in which the ith stick is L[i] units long. A stick can't be part of more than one pair of chopsticks. Help the Chef in pairing up the sticks to form the maximum number of usable pairs of chopsticks.

-----Input-----
The first line contains two space-separated integers N and D. The next N lines contain one integer each, the ith line giving the value of L[i].

-----Output-----
Output a single line containing the maximum number of pairs of chopsticks the Chef can form.

-----Constraints-----
- 1 ≤ N ≤ 100,000 (10 5 ) 
- 0 ≤ D ≤ 1,000,000,000 (10 9 ) 
- 1 ≤ L[i] ≤ 1,000,000,000 (10 9 ) for all integers i from 1 to N

-----Example-----
Input:

5 2
1
3
3
9
4

Output:
2

-----Explanation-----

The 5 sticks have lengths 1, 3, 3, 9 and 4 respectively. The maximum allowed difference in the lengths of two sticks forming a pair is at most 2.
It is clear that the 4th stick (length 9) cannot be used with any other stick.
The remaining 4 sticks can can be paired as (1st and 3rd) and (2nd and 5th) to form 2 pairs of usable chopsticks.

Difficulty: EASY
Tags: Sorting

Greedy algorithms
Skill types: Sorting

Greedy algorithms

Starter code:

Solutions:
(N, D) = map(int, input().split())
s = []
for i in range(N):
	x = int(input(''))
	s.append(x)
s.sort()
x = y = 0
while y < N - 1:
	if s[y + 1] - s[y] <= D:
		x += 1
		y += 2
	else:
		y += 1
print(x)


(n, d) = map(int, input().split())
l = []
for i in range(n):
	l.append(int(input()))
l.sort()
i = 0
count = 0
while i < n - 1:
	if l[i + 1] - l[i] <= d:
		count += 1
		i += 1
	i += 1
print(count)


(n, d) = map(int, input().split())
l = []
for i in range(n):
	l.append(int(input()))
l.sort()
count = 0
i = 0
if n > 1:
	while True:
		if l[i + 1] - l[i] <= d:
			count += 1
			i += 2
			if i >= n - 1:
				break
		else:
			i += 1
			if i >= n - 1:
				break
print(count)


(N, D) = [int(x) for x in input().split()]
b = []
for i in range(N):
	x = int(input(''))
	b.append(x)
b.sort()
a1 = 0
a2 = 0
while a2 < N - 1:
	if b[a2 + 1] - b[a2] <= D:
		a1 += 1
		a2 += 2
	else:
		a2 += 1
print(a1)


(n, d) = map(int, input().split())
l = []
for i in range(n):
	l.append(int(input()))
l.sort()
i = 1
c = 0
while i < n:
	if l[i] - l[i - 1] <= d:
		c += 1
		i += 2
	else:
		i += 1
print(c)


(n, d) = [int(i) for i in input().split()]
prev = 0
count = 0
k = []
for i in range(n):
	k.append(int(input()))
k = sorted(k)
i = 0
while i < n - 1:
	prev = count
	if k[i + 1] - k[i] <= d:
		count += 1
	if count == prev:
		i += 1
	else:
		i += 2
print(count)


(N, D) = map(int, input().split())
L = [0] * N
for i in range(N):
	L[i] = int(input())
L.sort()
ans = 0
j = 0
while j < N - 1:
	if L[j + 1] - L[j] <= D:
		j += 2
		ans += 1
	else:
		j += 1
print(ans)


def solve(n, s, a):
	a = sorted(a)
	cnt = 0
	i = 0
	while i < n - 1:
		if a[i + 1] - a[i] <= s:
			cnt += 1
			i += 2
		else:
			i += 1
	return cnt
(n, s) = map(int, input().split())
a = []
for i in range(n):
	a.append(int(input()))
print(solve(n, s, a))


(a, b) = map(int, input().split())
l = []
c = 0
for i in range(a):
	l.append(int(input()))
l.sort()
i = 0
while i <= a - 2:
	if l[i + 1] - l[i] <= b:
		c += 1
		i += 2
	else:
		i += 1
print(c)


(n, m) = map(int, input().split())
arr = []
for i in range(n):
	arr.append(int(input()))
arr.sort()
c = 0
i = 0
while i < n - 1:
	if arr[i + 1] - arr[i] <= m:
		c = c + 1
		i = i + 1
	i = i + 1
print(c)


(N, D) = [int(x) for x in input().split()]
L = []
for i in range(N):
	l = int(input())
	L.append(l)
c = 0
j = 0
L.sort()
while j < N - 1:
	if abs(L[j] - L[j + 1]) <= D:
		c = c + 1
		j = j + 2
	else:
		j = j + 1
print(c)


(n, d) = list(map(int, input().split()))
l = list()
pair = 0
for _ in range(n):
	l.append(int(input()))
l.sort()
while len(l) != 0:
	if len(l) == 1:
		break
	if abs(l[0] - l[1]) <= d:
		pair += 1
		l.pop(0)
		l.pop(0)
	else:
		l.pop(0)
print(pair)


(n, d) = map(int, input().split(' '))
l = []
for _ in range(n):
	l.append(int(input()))
l = sorted(l)
count = 0
i = 0
while i < n:
	try:
		if l[i + 1] - l[i] <= d:
			count = count + 1
			i = i + 1
	except:
		pass
	i = i + 1
print(count)


(N, D) = map(int, input().split())
L = []
for i in range(N):
	L.append(int(input()))
L.sort()
i = 0
pair = 0
while i < len(L) - 1:
	if L[i + 1] - L[i] <= D:
		pair += 1
		i += 1
	i += 1
print(pair)


(n, d) = map(int, input().split())
li = []
for i in range(n):
	li.append(int(input()))
li.sort()
cnt = 0
i = n - 1
while i > 0:
	if li[i] - li[i - 1] <= d:
		cnt += 1
		i -= 2
	else:
		i -= 1
print(cnt)


(n, k) = [int(d) for d in input().split()]
a = []
for i in range(n):
	a.append(int(input()))
a.sort()
count = 0
i = 0
while i < n - 1:
	if a[i + 1] - a[i] <= k:
		count += 1
		i += 1
	i += 1
print(count)


(n, d) = map(int, input().split(' '))
l = []
c = 0
for _ in range(n):
	L = int(input())
	l.append(L)
l.sort()
i = 1
while i < n:
	if l[i] - l[i - 1] <= d:
		c += 1
		i += 1
	i += 1
print(c)


(N, D) = map(int, input().split())
chopsticks = sorted([int(input()) for _ in range(N)])
p = 0
c = 0
while p < len(chopsticks) - 1:
	if chopsticks[p + 1] - chopsticks[p] <= D:
		c += 1
		p += 2
	else:
		p += 1
print(c)


(n, d) = map(int, input().split())
l = []
for i in range(n):
	length = int(input())
	l.append(length)
l = sorted(l)
i = 0
count = 0
while i < n - 1:
	if abs(l[i + 1] - l[i]) <= d:
		count += 1
		i += 2
	else:
		i += 1
print(count)


(N, D) = list(map(int, input().split()))
l = []
for i in range(N):
	l.append(int(input()))
l.sort()
c = 0
while len(l) != 0:
	if len(l) == 1:
		break
	if l[1] - l[0] <= D:
		l.pop(0)
		l.pop(0)
		c += 1
	else:
		l.pop(0)
print(c)


(n, d) = map(int, input().split())
l = []
ans = 0
for i in range(n):
	l.append(int(input()))
l.sort()
i = 0
while i < len(l) - 1:
	if abs(l[i + 1] - l[i]) <= d:
		ans += 1
		i += 2
	else:
		i += 1
print(ans)


def main():
	(n, d) = map(int, input().split())
	l = list()
	for i in range(n):
		l.append(int(input()))
	l.sort()
	ans = 0
	j = 0
	while j < n - 1:
		if l[j + 1] - l[j] <= d:
			ans += 1
			j += 1
		j += 1
	print(ans)
main()


def cp(l, d):
	l.sort()
	count = 0
	i = 0
	while i < len(l) - 1:
		if l[i + 1] - l[i] <= d:
			count += 1
			i += 2
		else:
			i += 1
	return count
(n, d) = map(int, input().split())
l = list((int(input()) for _ in range(n)))
print(cp(l, d))


(n, d) = map(int, input().split())
l = []
for i in range(n):
	l.append(int(input()))
l = sorted(l)
p = 0
b = 0
for i in range(1, n):
	if l[i] > l[i - 1] + d:
		p += (i - b) // 2
		b = i
p += (n - b) // 2
print(p)


def merge_sort(arr):
	if len(arr) > 1:
		mid = len(arr) // 2
		l = arr[:mid]
		r = arr[mid:]
		merge_sort(l)
		merge_sort(r)
		i = j = k = 0
		while i < len(l) and j < len(r):
			if l[i] < r[j]:
				arr[k] = l[i]
				i += 1
			else:
				arr[k] = r[j]
				j += 1
			k += 1
		while i < len(l):
			arr[k] = l[i]
			i += 1
			k += 1
		while j < len(r):
			arr[k] = r[j]
			j += 1
			k += 1

def main():
	(N, D) = map(int, input().split(' '))
	arr = []
	for i in range(N):
		arr.append(int(input()))
	merge_sort(arr)
	arr.sort()
	l = 0
	count = 0
	while l < N - 1:
		if arr[l + 1] - arr[l] <= D:
			count += 1
			l += 2
		else:
			l += 1
	print(count)
main()


(n, t) = map(int, input().split())
(a, d) = ([], 0)
for i in range(n):
	a.append(int(input()))
a.sort()
i = 0
while i < n - 1:
	if a[i + 1] - a[i] <= t:
		d += 1
		i += 1
	i += 1
print(d)


a = []
(N, D) = [int(x) for x in input().split()]
i = 0
while i < N:
	y = input()
	a.append(y)
	i = i + 1
A = [int(x) for x in a]
A.sort()
i = 0
count = 0
while i < N - 1:
	if A[i + 1] - A[i] <= D:
		i = i + 2
		count = count + 1
	else:
		i = i + 1
print(count)


(n, d) = map(int, input().split())
l = []
for i in range(0, n):
	a = int(input())
	l.append(a)
l.sort()
pairs = 0
while len(l) > 1:
	if l[1] - l[0] <= d:
		pairs += 1
		l.remove(l[0])
		l.remove(l[0])
	else:
		l.remove(l[0])
print(pairs)


(n, k) = input().split()
n = int(n)
k = int(k)
l = [0] * n
for x in range(n):
	l[x] = int(input())
l.sort()
c = 0
i = 0
while i < n - 1:
	if l[i + 1] - l[i] <= k:
		i += 1
		c += 1
	i += 1
print(c)


(N, K) = map(int, input().split(' '))
arr = list()
for i in range(N):
	arr.append(int(input()))
arr.sort()
count = 0
i = 0
while i < N - 1:
	if arr[i] >= arr[i + 1] - K:
		count += 1
		i += 2
	else:
		i += 1
print(count)


(n, d) = input().split()
sticks = []
for x in range(0, int(n)):
	chop = int(input())
	sticks.append(chop)
sticks.sort()
pair = 0
pairs = 0
for y in range(0, int(n)):
	if y > 0:
		if sticks[y] - sticks[y - 1] <= int(d) and pair == 0:
			pairs = pairs + 1
			pair = 1
		else:
			pair = 0
print(pairs)


(n, g) = map(int, input().split())
arr = [0] * n
for i in range(n):
	arr[i] = int(input())
arr.sort()
c = 0
i = 0
while i < n - 1:
	if arr[i + 1] - arr[i] <= g:
		c += 1
		i += 2
	else:
		i += 1
print(c)


(n, d) = map(int, input().split())
sticks = sorted([int(input()) for i in range(n)])
pairs = 0
i = 0
while i < n - 1:
	if sticks[i + 1] - sticks[i] <= d:
		pairs += 1
		i += 2
	else:
		i += 1
print(pairs)


l = list(map(int, input().split(' ')))
(n, d) = (l[0], l[1])
n_list = []
for i in range(n):
	n_list.append(int(input()))
n_list.sort()
knt = 0
i = 1
while i < n:
	if n_list[i] - n_list[i - 1] <= d:
		knt += 1
		i += 2
		continue
	i += 1
print(knt)


(total, diff) = list(map(int, input().split()))
l1 = [int(input()) for m in range(total)]
count = 0
l1.sort()
i = 0
while i < total - 1:
	if l1[i + 1] - l1[i] <= diff:
		count += 1
		i += 2
	else:
		i += 1
print(count)


[N, D] = list(map(int, input().split()))
L = []
for _ in range(N):
	L.append(int(input()))
L.sort()
(count, i) = (0, 0)
while i < N - 1:
	if L[i + 1] - L[i] <= D:
		count += 1
		i += 2
	else:
		i += 1
print(count)


def max_pairs(n, d, L):
	L.sort()
	count = 0
	i = 0
	while i < n - 1:
		if L[i + 1] - L[i] <= d:
			count += 1
			i += 2
		else:
			i += 1
	return count
(n, d) = map(int, input().split())
L = []
for i in range(n):
	L.append(int(input()))
print(max_pairs(n, d, L))


(n, d) = map(int, input().split(' '))
stick = []
(pairs, i) = (0, 0)
for _ in range(n):
	stick.append(int(input()))
stick.sort()
while i < n - 1:
	if stick[i + 1] - stick[i] <= d:
		pairs += 1
		i += 2
	else:
		i += 1
print(pairs)


(n, d) = map(int, input().split())
arr = []
for i in range(n):
	a = int(input())
	arr.append(a)
arr.sort()
ans = 0
i = 1
while i < len(arr):
	if arr[i] - arr[i - 1] <= d:
		ans += 1
		i += 2
	else:
		i += 1
print(ans)


(N, D) = map(int, input().split())
l = []
for i in range(N):
	l.append(int(input()))
l.sort()
cnt = 0
b = 0
while b < N - 1:
	if l[b + 1] - l[b] <= D:
		cnt += 1
		b += 1
	b += 1
print(cnt)


(n, d) = map(int, input().split())
s = sorted([int(input()) for _ in range(n)])
p = 0
i = 0
while i < n - 1:
	if s[i + 1] - s[i] <= d:
		p += 1
		i += 2
	else:
		i += 1
print(p)


(N, D) = map(int, input().split(' '))
L = []
for n in range(N):
	L.append(int(input()))
L = sorted(L)
res = 0
i = 0
while i < len(L) - 1:
	if L[i + 1] - L[i] <= D:
		res += 1
		i += 1
	i += 1
print(res)


(a, b) = list(map(int, input().split()))
ls = []
for i in range(a):
	ls.append(int(input()))
ls.sort()
c = 0
i = 0
while i < a - 1:
	if ls[i + 1] - ls[i] <= b:
		c = c + 1
		i = i + 1
	i = i + 1
print(c)


(n, d) = [int(x) for x in input().split()]
ls = []
for i in range(n):
	li = int(input())
	ls.append(li)
count = 0
i = 0
ls.sort()
while i < n - 1:
	if ls[i + 1] - ls[i] <= d:
		count += 1
		i += 1
	i += 1
print(count)


(n, d) = list(map(int, input().split()))
sticks = [0] * n
for i in range(n):
	sticks[i] = int(input())
sticks.sort()
ans = 0
i = 0
while i < len(sticks) - 1:
	if sticks[i + 1] - sticks[i] <= d:
		ans += 1
		i += 1
	i += 1
print(ans)


(N, D) = [int(i) for i in input().split(' ')]
L = []
for _2 in range(N):
	L.append(int(input()))
L.sort()
ans = 0
last = L[0]
i = 1
while i < N:
	if abs(L[i] - last) <= D:
		ans += 1
		if i + 1 >= N:
			break
		last = L[i + 1]
		i += 2
	else:
		last = L[i]
		i += 1
print(ans)


(n, k) = map(int, input().split())
l = []
for i in range(n):
	l.append(int(input()))
l.sort()
count = 0
i = 0
while i < n - 1:
	if l[i + 1] - l[i] <= k:
		count += 1
		i += 2
	else:
		i += 1
print(count)


(n, d) = [int(i) for i in input().split()]
L = []
for _ in range(n):
	L.append(int(input()))
L.sort(reverse=True)
count = 0
i = 0
while i < n - 1:
	if L[i] - L[i + 1] <= d:
		count += 1
		i += 2
	else:
		i += 1
print(count)


(n, d) = map(int, input().split())
arr = [] * n
for _ in range(n):
	arr.append(int(input()))
arr.sort()
pairs = 0
i = 0
while i < n - 1:
	if arr[i + 1] - arr[i] > d:
		i += 1
		continue
	pairs += 1
	i += 2
print(pairs)


c = input().split()
c = [int(c[0]), int(c[1])]
l = []
for i in range(c[0]):
	l.append(int(input()))
l.sort()
co = 0
i = 1
while i < c[0]:
	if l[i] - l[i - 1] <= c[1]:
		co += 1
		i += 1
	i += 1
print(co)


(a, b) = list(map(int, input().split()))
l = []
for _ in range(a):
	x = int(input())
	l.append(x)
l.sort()
m = 0
i = 0
while i < a - 1:
	x = l[i + 1] - l[i]
	if x <= b:
		m = m + 1
		i = i + 2
	else:
		i = i + 1
print(m)


t = [int(i) for i in input().split()]
arr = []
ans = 0
for i in range(t[0]):
	arr.append(int(input()))
arr.sort()
i = 0
while i < len(arr) - 1:
	if arr[i + 1] - arr[i] <= t[1]:
		ans += 1
		i += 2
	else:
		i += 1
print(ans)


(n, m) = map(int, input().split())
l = []
for _ in range(n):
	l.append(int(input()))
l = sorted(l)
c = 0
i = 0
while i + 1 < n:
	if abs(l[i] - l[i + 1]) <= m:
		c += 1
		i += 2
	else:
		i += 1
print(c)


(N, D) = list(map(int, input().split()))
s = []
for _ in range(N):
	s.append(int(input()))
s.sort()
ans = 0
i = 0
while i < len(s) - 1:
	if s[i + 1] - s[i] <= D:
		ans += 1
		i += 1
	i += 1
print(ans)


import string
from functools import lru_cache
import atexit, io
import sys
from math import log2
import math
from heapq import heappush, heappop, heapify
from bisect import bisect_left, bisect_right, insort
from math import gcd
from math import sqrt
from collections import Counter, defaultdict
from heapq import heappush, heappop, heapify
import heapq

class FenwickTree:

	def __init__(self, n):
		self.farr = [0] * (n + 1)

	def prefixSum(self, idx):
		s = 0
		while idx > 0:
			s += self.farr[idx]
			idx -= idx & -idx
		return s

	def update(self, idx, val):
		while idx < len(self.farr):
			self.farr[idx] += val
			idx += idx & -idx

	def find(self, k):
		curr = 0
		ans = 0
		prevSum = 0
		for i in range(log2(n), -1, -1):
			if self.farr[curr + (i << i)] + prevSum < k:
				curr += 1 << i
				prevSum += self.farr[curr]
		return curr + 1

class Trie(object):

	def __init__(self):
		self.trie = {}

	def insert(self, word):
		t = self.trie
		for c in word:
			if c not in t:
				t[c] = {}
			t = t[c]
		t['-'] = True

	def search(self, word):
		t = self.trie
		for c in word:
			if c not in t:
				return False
			t = t[c]
		return '-' in t

	def startsWith(self, prefix):
		t = self.trie
		for c in prefix:
			if c not in t:
				return False
			t = t[c]
		return True

	def query(self, word):
		ans = 0
		t = self.trie
		for i in word:
			if i not in t:
				break
			ans += 1
			t = t[i]
		return ans

class FenvTree:

	def __init__(self, arr):
		arr.insert(0, 0)
		self.farr = [0]
		prefix = [0]
		for i in arr[1:]:
			prefix.append(prefix[-1] + i)
		for i in range(1, len(arr)):
			idash = i - (i & -i)
			self.farr.append(prefix[i] - prefix[idash])

	def query(self, l, r):
		return self.prefixSum(r) - self.prefixSum(l - 1)

	def prefixSum(self, idx):
		s = 0
		while idx > 0:
			s += self.farr[idx]
			idx -= idx & -idx
		return s

	def update(self, idx, val):
		while idx < len(self.farr):
			self.farr[idx] += val
			idx += idx & -idx

class DSU:

	def __init__(self, n):
		self.parent = []
		self.size = []
		for i in range(n + 1):
			self.parent.append(i)
			self.size.append(1)

	def union(self, u, v):
		pu = self.findPar(u)
		pv = self.findPar(v)
		if pu == pv:
			return
		if self.size[pu] < self.size[pv]:
			self.parent[pu] = pv
			self.size[pv] += self.size[pu]
		else:
			self.parent[pv] = pu
			self.size[pu] += self.size[pv]

	def unionByRank(self, u, v):
		pu = self.findPar(u)
		pv = self.findPar(v)
		if pu == pv:
			return
		if self.rank[pu] < self.rank[pv]:
			self.parent[pu] = pv
		elif self.rank[pv] < self.rank[pu]:
			self.parent[pv] = pu
		else:
			self.parent[pu] = pv
			self.rank[pv] += 1

	def findPar(self, node):
		if self.parent[node] == node:
			return node
		self.parent[node] = self.findPar(self.parent[node])
		return self.parent[node]

class info:

	def __init__(self, _open=0, _close=0, _full=0):
		self.open = _open
		self.close = _close
		self.full = _full

class SGTree:

	def __init__(self, n):
		self.seg = [0 for _ in range(4 * n)]

	def build(self, ind, low, high, arr):
		if low == high:
			self.seg[ind] = arr[low]
			return
		mid = (low + high) // 2
		self.build(2 * ind + 1, low, mid, arr)
		self.build(2 * ind + 2, mid + 1, high, arr)
		self.seg[ind] = min(self.seg[2 * ind + 1], self.seg[2 * ind + 2])

	def query(self, ind, low, high, l, r):
		if r < low or l > high:
			return sys.maxsize
		if low <= l and r >= high:
			return self.seg[ind]
		mid = low + high >> 1
		left = self.query(2 * ind + 1, 0, mid, l, r)
		right = self.query(2 * ind + 2, mid + 1, high, l, r)
		return min(left, right)

	def update(self, ind, low, high, i, val):
		if low == high:
			self.seg = val
			return
		mid = low + high >> 1
		if i <= mid:
			self.update(2 * ind + 1, low, mid, i, val)
		else:
			self.update(2 * ind + 1, mid + 1, high, i, val)
		self.seg[ind] = min(self.seg[2 * ind + 1], self.seg[2 * ind + 2])

def isPalindrome(s):
	i = 0
	j = len(s) - 1
	while i < j:
		if s[i] != s[j]:
			return False
		i += 1
		j -= 1
	return True

def IntList():
	return list(map(int, sys.stdin.readline().strip().split()))

def getInts():
	return map(int, sys.stdin.readline().strip().split())

def getStrs():
	return sys.stdin.readline().strip()

def StrList():
	list(sys.stdin.readline().strip())

def FloatList():
	lis = input().split()
	lis = list(map(float, lis))
	return lis

def SieveOfEratosthenes(n, isPrime):
	isPrime[0] = isPrime[1] = False
	for i in range(2, n + 1):
		isPrime[i] = True
	p = 2
	while p * p <= n:
		if isPrime[p]:
			i = p * p
			while i <= n:
				isPrime[i] = False
				i += p
		p += 1

def findPrimePair(n):
	isPrime = [0] * (n + 1)
	SieveOfEratosthenes(n, isPrime)
	for i in range(0, n):
		if isPrime[i] and isPrime[n - i]:
			print(i, n - i)
			return

def query(ind, low, high, l, r, seg):
	if r < low or l > high:
		return info()
	if low <= l and r >= high:
		return self.seg[ind]
	mid = low + high >> 1
	left = self.query(2 * ind + 1, 0, mid, l, r, seg)
	right = self.query(2 * ind + 2, mid + 1, high, l, r, seg)
	return merge(left, right)

def merge(left, right):
	ans = info()
	ans.full = left.full + right.full + min(left.open, right.close)
	ans.open = left.open + right.open + min(left.open, right.close)
	ans.close = left.close + right.close + min(left.open, right.close)
	return ans

def build(ind, low, high, s, seg):
	if low == high:
		seg[ind] = info(s[low] == '(', s[low] == ')', 0)
		return
	mid = (low + high) // 2
	build(2 * ind + 1, low, mid, s, seg)
	build(2 * ind + 2, mid + 1, high, s, seg)
	seg[ind] = merge(seg[2 * ind + 1], seg[2 * ind + 2])

def countN(lis):
	ans = 0
	cnt = 0
	for i in range(len(lis) - 1, -1, -1):
		if lis[i] == 0:
			cnt += 1
		else:
			ans += cnt
	return ans

class Pair:

	def __init__(self, x, y, gcd):
		self.x = x
		self.y = y
		self.gcd = gcd

def Euclidean(a, b):
	if b == 0:
		return Pair(1, 0, a)
	dash = Euclidean(b, a % b)
	return Pair(dash.y, dash.x - a // b * dash.y, dash.gcd)

def chineseRemainder(a1, n1, a2, n2):
	p = Euclidean(n1, n2)
	x = p.x
	gcd = p.gcd
	if (a1 - a2) % gcd:
		return -1
	k = (a1 - a2) // gcd
	a = a1 - n1 * (k * x % n2 // gcd)
	lcm = n1 * n2 // gcd
	if a < 0:
		a += lcm
	return a
mdd = 998244353

def ncr(n, k, md):
	mod = 998244353
	tab = [[0 for _ in range(k + 1)] for i in range(n + 1)]
	for i in range(1, len(tab)):
		tab[i][0] = 1
	for i in range(1, len(tab)):
		for j in range(1, min(i + 1, len(tab[0]))):
			if i == 1 and j == 1:
				tab[i][j] = 1
			else:
				tab[i][j] = (tab[i - 1][j] + tab[i - 1][j - 1]) % mod
	return tab

def repeat(i, j, arr):
	if i > j:
		return
	cnt1 = i
	cnt2 = j
	while cnt1 < cnt2:
		(arr[cnt1], arr[cnt2]) = (arr[cnt2], arr[cnt1])
		cnt1 += 1
		cnt2 -= 1
	if i - 1 >= 0:
		ele = arr[i - 1]
		if ele > 0:
			s = int(sqrt(ele - 1)) + 1
			first = s * s - ele
			repeat(first, i - 1, arr)
	return
spf = []

def makeSeive(n):
	for i in range(n + 1):
		spf.append(i)

def seive(n):
	s = int(sqrt(n + 1))
	for i in range(2, s + 1):
		if spf[i] == i:
			for j in range(i * i, n + 1, i):
				if spf[j] == j:
					spf[j] = i

def getFactorization(x):
	ret = set()
	while x != 1:
		ret.add(spf[x])
		x = x // spf[x]
	return ret

def zFunc(patt, txt):
	st = patt + '#' + txt
	zArray = [0 for _ in range(len(st))]
	l = 0
	r = 0
	for i in range(1, len(st)):
		if r >= i:
			zArray[i] = min(r - i + 1, zArray[i - l])
		while i + zArray[i] < len(st) and st[zArray[i]] == st[zArray[i] + i]:
			zArray[i] += 1
		if zArray[i] + i - 1 > r:
			l = i
			r = i + zArray[i] - 1
	return zArray.count(len(patt))

def kmp(txt, patt):
	newString = patt + '#' + txt
	freqCount = 0
	freqArray = [0 for _ in range(len(newString))]
	i = 1
	length = 0
	while i < len(newString):
		if newString[i] == newString[length]:
			length += 1
			freqArray[i] = length
			i += 1
		elif length > 0:
			length = freqArray[length - 1]
		else:
			freqArray[i] = 0
			i += 1
	for i in freqArray:
		if i == len(patt):
			freqCount += 1
	return freqCount

def getLPS(s):
	st = '@#' + '#'.join(s) + '#&'
	c = 0
	r = 0
	lps = [0] * len(st)
	for i in range(1, len(st) - 1):
		mirror = c - (i - c)
		if i < r and mirror >= 0:
			lps[i] = min(lps[mirror], r - i)
		while st[i + lps[i] + 1] == st[i - lps[i] - 1]:
			lps[i] += 1
		if i + lps[i] > r:
			r = i + lps[i]
			c = i
	return max(lps)
fact = [1 for _ in range(100002)]
mod = 10 ** 9 + 7

def getFact(n):
	for i in range(1, n + 1):
		fact[i] = fact[i - 1] * i % mod

def getInv(word):
	num = math.factorial(len(word))
	for v in Counter(word).values():
		num = num * pow(math.factorial(v), mod - 2, mod) % mod
	return int(num) % (10 ** 9 + 7)

def subarrayXor(arr, n, m):
	HashTable = defaultdict(bool)
	HashTable[0] = 1
	count = 0
	curSum = 0
	for i in arr:
		curSum ^= i
		if HashTable[curSum ^ m]:
			count += HashTable[curSum ^ m]
		HashTable[curSum] += 1
	return count

def get2dPrefixSum(matrix, mid=0):
	n = len(matrix)
	m = len(matrix[0])
	prefix = [[0 for _ in range(m + 1)] for i in range(n + 1)]
	for i in range(1, n + 1):
		for j in range(1, m + 1):
			prefix[i][j] = prefix[i - 1][j] + prefix[i][j - 1] - prefix[i - 1][j - 1] + matrix[i - 1][j - 1]
	return prefix

def get2dPrefixSumCnt(matrix, mid=0):
	n = len(matrix)
	m = len(matrix[0])
	prefix = [[0 for _ in range(m + 1)] for i in range(n + 1)]
	for i in range(1, n + 1):
		for j in range(1, m + 1):
			prefix[i][j] = prefix[i - 1][j] + prefix[i][j - 1] - prefix[i - 1][j - 1] + matrix[i - 1][j - 1]
	return prefix

def getSumRange(r1, c1, r2, c2, prefix):
	cnt = prefix[r2][c2] - prefix[r2][c1 - 1] - prefix[r1 - 1][c2] + prefix[r1 - 1][c1 - 1]
	return cnt

def getPrefix(arr):
	prefix = [0]
	for i in arr:
		prefix.append(prefix[-1] + i)
	return prefix

def getInv(arr):
	cnt = 0
	ans = []
	for i in arr:
		ind = bisect_right(ans, i)
		cnt += len(ans) - ind
		insort(ans, i)
	return cnt
M = 10 ** 5

def getFactors(x):
	temp = []
	k = 1
	while k * k <= x:
		if x % k == 0:
			temp.append(k)
			if k * k != x:
				temp.append(x // k)
		k += 1
	return temp

def grayCode(n):
	if not n:
		return [0]
	res = [0, 1]
	for i in range(2, n + 1):
		for j in range(len(res) - 1, -1, -1):
			res.append(res[j] | 1 << i - 1)
	return res

def threeSum(nums, target):
	ans = -1
	d = dict()
	for (i, j) in enumerate(nums):
		d[j] = i + 1
	nums.sort()
	for i in range(len(nums) - 2):
		low = i + 1
		high = len(nums) - 1
		while low < high:
			s = nums[i] + nums[low] + nums[high]
			if s == target:
				print(d[nums[low]], d[nums[high]], d[nums[i]])
				return
				low += 1
				high -= 1
			elif s < 0:
				low = low + 1
			else:
				high -= 1
	print('IMPOSSIBLE')
	return

def FuckCF():
	(n, m) = getInts()
	lis = []
	for _ in range(n):
		lis.append(int(input()))
	lis.sort()
	cnt = 0
	i = 1
	while i < n:
		if lis[i] - lis[i - 1] <= m:
			cnt += 1
			i += 1
		i += 1
	print(cnt)
t = 1
for i in range(t):
	FuckCF()


(n, d) = map(int, input().split())
L = []
for _ in range(n):
	L.append(int(input()))
L = sorted(L)
last_break = 0
pairs = 0
for i in range(1, n):
	if L[i] > L[i - 1] + d:
		pairs += (i - last_break) // 2
		last_break = i
pairs += (n - last_break) // 2
print(pairs)


(N, D) = map(int, input().split())
li = []
for i in range(N):
	li.append(int(input()))
li.sort()
count = 0
i = 0
while i + 1 < N:
	if abs(li[i + 1] - li[i]) <= D:
		count += 1
		i += 2
	else:
		i += 1
print(count)


(n, d) = map(int, input().split())
a = [0] * n
for i in range(n):
	a[i] = int(input())
a.sort()
count = 0
i = 0
while i < n - 1:
	if a[i + 1] - a[i] <= d:
		i += 2
		count += 1
	else:
		i += 1
print(count)


(n, k) = map(int, input().split())
Chop_l = []
count = 0
for i in range(n):
	Chop_l.append(int(input()))
Chop_l.sort()
(x, y) = (0, 0)
while y < n - 1:
	if Chop_l[y + 1] - Chop_l[y] <= k:
		x += 1
		y += 2
	else:
		y += 1
print(x)


try:
	(N, D) = map(int, input().split())
	space = []
	for __ in range(N):
		space.append(int(input()))
	space.sort()
	i = 0
	score = 0
	j = 1
	while i < len(space):
		if i < len(space) - 1 and j < len(space):
			a = space[i]
			b = space[j]
			if b - a <= D:
				i += 2
				j += 2
				score += 1
			else:
				i += 1
				j += 1
		else:
			break
	print(score)
except:
	pass


(n, d) = map(int, input().split())
l = sorted([int(input()) for i in range(n)])
x = 0
j = 0
while j < n - 1:
	if l[j + 1] - l[j] <= d:
		x += 1
		j += 2
	else:
		j += 1
print(x)


(n, d) = map(int, input().split())
l = [0] * n
for x in range(n):
	l[x] = int(input())
l.sort()
i = 1
a = 0
while i <= n - 1:
	if abs(l[i] - l[i - 1]) <= d:
		a += 1
		i += 2
	else:
		i += 1
print(a)


(n, a) = map(int, input().split())
l = []
for i in range(n):
	l.append(int(input()))
l.sort()
c = 0
i = 0
while i < n - 1:
	if l[i + 1] - l[i] <= a:
		c += 1
		i += 1
	i += 1
print(c)


a = []
(n, d) = [int(x) for x in input().split()]
for i in range(n):
	a.append(int(input()))
a.sort()
(i, c) = (0, 0)
while i < n - 1:
	if a[i + 1] - a[i] <= d:
		c += 1
		i += 1
	i += 1
print(c)


(N, D) = list(map(int, input().split()))
L = []
for _ in range(N):
	L.append(int(input()))
L.sort()
ans = 0
i = 0
while i < len(L) - 1:
	diff = L[i + 1] - L[i]
	if diff <= D:
		ans += 1
		i += 2
	else:
		i += 1
print(ans)


x = input().split()
n = int(x[0])
d = int(x[1])
l = []
for i in range(n):
	l.append(int(input()))
l.sort()
count = 0
i = 0
while i < n - 1:
	if l[i + 1] - l[i] <= d:
		count += 1
		i += 2
	else:
		i += 1
print(count)


(n, allowed) = list(map(int, input().split()))
sticks = []
for i in range(n):
	sticks.append(int(input()))
sticks.sort()
cnt = 0
s = set()
for i in range(1, n):
	if sticks[i] - sticks[i - 1] <= allowed and i not in s and (i - 1 not in s):
		cnt += 1
		s.add(i)
		s.add(i - 1)
print(cnt)


(n, d) = map(int, input().split())
length = []
for _ in range(n):
	length.append(int(input()))
length.sort()
i = 0
ans = 0
while i < n - 1:
	if length[i + 1] - length[i] <= d:
		ans += 1
		i += 2
	else:
		i += 1
print(ans)