from sys import stdin
from bisect import *
from math import factorial


def nCr(n, r):
    f, m = factorial, 1
    for i in range(n, n - r, -1):
        m *= i
    return int(m // f(r))


def arr_inp(n):
    if n == 1:
        return [int(x) for x in stdin.readline().split()]
    elif n == 2:
        return [float(x) for x in stdin.readline().split()]
    else:
        return [str(x) for x in stdin.readline().split()]


n, d = arr_inp(1)
a, ans = arr_inp(1), 0

for i in range(2, n):
    en = bisect_left(a, a[i] - d)
    ans += nCr(i - en, 2)

print(ans)
