#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int a, d, n;
  vector<long long int> v;
  cin >> n >> d;
  while (n--) {
    cin >> a;
    v.push_back(a);
  }
  long long int ans = 0;
  for (int i = 0; i < v.size(); i++) {
    long long int pos = upper_bound(v.begin(), v.end(), v[i] + d) - v.begin();
    pos = pos - i - 1;
    if (pos > 1) {
      ans += (pos * (pos - 1)) >> 1;
    }
  }
  cout << ans << endl;
}
