#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, p, d, ct = 0;
  cin >> n >> d;
  vector<long long> v(n);
  for (int i = 0; i < n; i++) cin >> v[i];
  vector<long long>::iterator l, h;
  for (l = v.begin(); l < v.end() - 2; l++) {
    h = upper_bound(l, v.end(), *l + d);
    if (h - l > 2) {
      p = h - l - 2;
      ct = ct + p * (p + 1) / 2;
    }
  }
  cout << ct;
  return 0;
}
