#include <bits/stdc++.h>
using namespace std;
deque<int> s;
int main() {
  ios::sync_with_stdio(false);
  int n, d;
  cin >> n >> d;
  int tmp;
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    cin >> tmp;
    s.push_back(tmp);
    while (s.back() - s.front() > d) {
      ans += (s.size() > 3
                  ? ((long long)(s.size() - 2) * (long long)(s.size() - 3) / 2)
                  : 0);
      s.pop_front();
    }
  }
  while (!s.empty()) {
    ans += (s.size() > 2
                ? ((long long)(s.size() - 1) * (long long)(s.size() - 2) / 2)
                : 0);
    s.pop_front();
  }
  cout << ans << endl;
  return 0;
}
