#include <bits/stdc++.h>
using namespace std;
int N = 1000001;
long long ans = 0;
bool ispossible(vector<int> &v, int mid, int e, int i, int d,
                long long &curans) {
  if (i - mid + 1 < 3 || mid < 0 || e < 0) {
    return false;
  }
  if (v[i] - v[mid] <= d) {
    long long n = i - mid;
    curans = n * (n - 1) / 2;
    return true;
  }
  return false;
}
void solve() {
  int n;
  cin >> n;
  ;
  int d;
  cin >> d;
  ;
  vector<int> v(n);
  for (int i = 0; i < n; i++) {
    cin >> v[i];
  }
  if (n < 3) {
    cout << 0 << endl;
    return;
  }
  long long curans = 0;
  for (int i = 0; i < n; i++) {
    int s = 0, e = i - 1;
    int mid;
    curans = 0;
    while (s <= e) {
      mid = s + (e - s) / 2;
      if (ispossible(v, mid, e, i, d, curans)) {
        e = mid - 1;
      } else {
        s = mid + 1;
      }
    }
    ans += curans;
  }
  cout << ans << endl;
  return;
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
