#include <bits/stdc++.h>
using namespace std;
long long int res(long long int x) {
  long long int ans = 0;
  if (x > 0) ans = (x * (x - 1)) / 2;
  return ans;
}
int main() {
  long int n;
  long long int d;
  cin >> n >> d;
  vector<long long int> vect;
  long long int x;
  for (long int i = 0; i < n; i++) {
    cin >> x;
    vect.push_back(x);
  }
  vector<long long int>::iterator it, it2;
  long long int ans = 0;
  for (long int i = 2; i < n; i++) {
    it = lower_bound(vect.begin(), vect.end(), vect[i] - d);
    if (it != vect.end()) {
      ans = ans + res(i - (it - vect.begin()));
    }
  }
  cout << ans;
  return 0;
}
