#include <bits/stdc++.h>
using namespace std;
long long nC3(int nn) {
  if (nn < 2) return 0;
  return (long long)nn * (nn - 1) / 2;
}
int main() {
  int n, k, tem;
  long long ans = 0;
  cin >> n >> k;
  vector<int> v;
  for (long long i = 0; i < n; i++) {
    cin >> tem;
    v.push_back(tem);
  }
  std::vector<int>::iterator upper1;
  for (long long i = 0; i < n; i++) {
    upper1 = upper_bound(v.begin(), v.end(), v[i] + k);
    int nn = (upper1 - v.begin());
    if (v[nn - 1] - v[i] > k) continue;
    nn -= i;
    nn--;
    ans += nC3(nn);
    nn += i;
    nn--;
    if (nn == n - 1) break;
  }
  cout << ans << "\n";
}
