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