#include <bits/stdc++.h>
using namespace std;
long long a[100330];
long long f(long long x) {
  if (x % 2)
    return (x - 1) / 2 * x;
  else
    return x / 2 * (x - 1);
}
int main() {
  long long ans, p, n, d;
  while (cin >> n >> d) {
    for (long long j = 0; j < n; j++) cin >> a[j];
    ans = 0;
    p = 2;
    for (long long j = 0; j < n - 2; j++) {
      while (p < n && a[p] - a[j] <= d) p++;
      if (p - j - 2 >= 0) ans += f(p - j - 1);
    }
    cout << ans << endl;
  }
}
