#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int n, d, count = 0;
  cin >> n >> d;
  long long int a[n];
  for (int i = 0; i < n; i++) cin >> a[i];
  long long int start = 0, end = 0;
  sort(a, a + n);
  while (end != n) {
    if ((a[end] - a[start]) <= d)
      end++;
    else {
      start++;
      if ((end - start) >= 2) {
        long long int k = end - start;
        count += k * (k - 1) / 2;
      }
    }
  }
  if ((end - start) >= 2) {
    long long int k = end - start;
    count += k * (k - 1) * (k - 2) / 6;
  }
  cout << count << endl;
}
