#include <bits/stdc++.h>
using namespace std;
int a[1000000], b[100000], c[100000], d[3];
bool cmp(int a, int b) { return a >= b; }
set<int> x;
int main() {
  int n, m, x, v, y, en;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    cin >> a[i];
  }
  sort(a + 1, a + 1 + n);
  if (n <= 2)
    cout << 0 << endl;
  else {
    long long ans = 0;
    for (int i = 1; i <= n - 2; i++) {
      y = upper_bound(a + 1, a + 1 + n, a[i] + m) - a - 1;
      ans += (long long)(y - i) * (y - i - 1) / 2;
    }
    cout << ans << endl;
  }
}
