#include <bits/stdc++.h>
using namespace std;
signed main() {
  long long n, d;
  cin >> n >> d;
  long long a[n + 1];
  for (long long i = 1; i <= n; i++) cin >> a[i];
  if (n <= 2) {
    cout << 0 << endl;
    return 0;
  }
  long long ans = 0;
  long long j = 0;
  for (long long i = 1; i <= n - 2; i++) {
    while (i + j <= n and a[i + j] - a[i] <= d) {
      j++;
    }
    j--;
    long long len = j;
    ans += (len) * (len - 1) / 2;
  }
  cout << ans << endl;
  return 0;
}
