#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, d, i, a[100002];
  unsigned long long cnt = 0;
  cin >> n >> d;
  for (i = 1; i <= n; ++i) cin >> a[i];
  for (i = 1; i <= n - 2; ++i) {
    int left = i, right = n, mij = 0;
    while (left <= right) {
      mij = (left + right) / 2;
      if (a[mij] - a[i] > d)
        right = mij - 1;
      else {
        if (a[mij + 1] - a[i] > d)
          break;
        else
          left = mij + 1;
      }
    }
    if (mij - i >= 2) {
      unsigned long long k = mij - i;
      cnt += (k - 1) * (k) / 2;
    }
  }
  cout << cnt;
  return 0;
}
