#include <bits/stdc++.h>
using namespace std;
int32_t main() {
  long long n, d;
  cin >> n >> d;
  vector<long long> v(n);
  for (long long i = 0; i < n; i++) {
    cin >> v[i];
  }
  long long ans = 0;
  for (long long i = 0; i + 2 < n; i++) {
    auto it = upper_bound(v.begin(), v.end(), v[i] + d) - v.begin() - 1;
    ans += (it - i) * (it - i - 1) / 2;
  }
  cout << ans << endl;
  return 0;
}
