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