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