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