#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, d;
  cin >> n >> d;
  vector<long long> v(n);
  for (auto &x : v) cin >> x;
  long long r = 0;
  for (auto i = v.begin(); i != v.end(); ++i) {
    long long m = upper_bound(v.begin(), v.end(), *i + d) - i - 1;
    if (m >= 2) r += m * (m - 1) / 2;
  }
  cout << r << endl;
  return 0;
}
