#include <bits/stdc++.h>
using namespace std;
const long long LLMAX = 0x7fffffffffffffffLL;
const long long LLMIN = 0x8000000000000000LL;
const int IMAX = 0x7fffffff;
const int IMIN = 0x80000000;
const double eps = 10e-7;
const int maxn = 100000 + 10;
int n;
long long d;
long long x[maxn];
int findPos(int idx) {
  int left = idx + 2, right = n, mid = (left + right) >> 1;
  int ret = 0;
  while (left <= right) {
    mid = (left + right) >> 1;
    if (x[mid] - x[idx] <= d) {
      ret = mid > ret ? mid : ret;
      left = mid + 1;
    } else {
      right = mid - 1;
    }
  }
  if (ret)
    return ret;
  else
    return 0;
}
int main() {
  while (~scanf("%d%I64d", &n, &d)) {
    long long ans = 0;
    for (int i = 1; i <= n; i++) {
      scanf("%I64d", &x[i]);
    }
    if (n == 1 || n == 2) {
      printf("0\n");
      continue;
    }
    for (int i = 1; i <= n - 2; i++) {
      long long temp = (long long)(findPos(i));
      if (temp) {
        temp = temp - (long long)i;
        ans += (temp * (temp - 1)) / 2LL;
      }
    }
    printf("%I64d\n", ans);
  }
  return 0;
}
