#include <bits/stdc++.h>
long long data[100010];
long long queue[100010];
long long head, tail;
int main() {
  long long n, m;
  long long i, j, k;
  scanf("%I64d%I64d", &n, &m);
  for (i = 1; i <= n; i++) scanf("%I64d", &data[i]);
  std::sort(data + 1, data + 1 + n);
  head = 1;
  tail = 0;
  long long resu = 0;
  for (i = 1; i <= n; i++) {
    queue[++tail] = data[i];
    while (queue[tail] - queue[head] > m) {
      resu += (tail - head - 1) * (tail - head - 2) / 2;
      head++;
    }
  }
  while (head <= tail) {
    resu += (tail - head) * (tail - head - 1) / 2;
    head++;
  }
  printf("%I64d\n", resu);
  return 0;
}
