from collections import defaultdict N, M = map(int, input().split()) A = list(map(int, input().split())) L = defaultdict(int) R = defaultdict(int) for a in A: R[a] += 1 ans = 0 for a in A: R[a] -= 1 ans += L[a-M]*R[a+M] L[a] += 1 print(ans)