#include <bits/stdc++.h>
using namespace std;
int i, i0, n, d, ans, a[100005];
int main() {
  scanf("%d %d", &n, &d);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  sort(a + 1, a + 1 + n);
  long long ans = 0;
  for (int i = 1, i0 = 1; i <= n; i++) {
    while (a[i] - a[i0] > d) i0++;
    long long p = i - i0;
    ans += p * (p - 1) / 2;
  }
  printf("%lld\n", ans);
  return 0;
}
