#include <bits/stdc++.h>
using namespace std;
long long s[100010];
int bs(int l, int r, long long key) {
  while (l <= r) {
    int m = (l + r) >> 1;
    if (s[m] <= key)
      l = m + 1;
    else
      r = m - 1;
  }
  return l;
}
int main() {
  long long n, d;
  while (cin >> n >> d) {
    for (int i = 1; i <= n; i++) cin >> s[i];
    sort(s + 1, s + 1 + n);
    long long ans = 0;
    for (int i = 1; i <= n; i++) {
      long long need = s[i] + d;
      int pos = bs(1, n, need) - 1;
      long long num = pos - i;
      if (num > 1) ans += num * (num - 1) / 2;
    }
    cout << ans << endl;
  }
  return 0;
}
