from sys import stdin input = stdin.readline import collections def solve(): N, D = map(int, input().split()) A = list(map(int, input().split())) right_cnt = collections.Counter(A) left_cnt= collections.Counter() ans = 0 for x in A: right_cnt[x] -= 1 val_i = x - D val_k = x + D count_i = left_cnt[val_i] count_k = right_cnt[val_k] if count_i > 0 and count_k > 0: ans += count_i * count_k left_cnt[x] += 1 print(ans) if __name__ == "__main__": solve()