#include <bits/stdc++.h>
using namespace std;
long long n, d;
vector<long long> num, maxdist;
long long check(long long a, long long b) {
  if (abs(a - b) > d) return false;
  return true;
}
long long comb(long long n) { return ((n) * (n - 1)) / 2; }
int main() {
  cin >> n >> d;
  num.resize(n);
  maxdist.resize(n);
  for (int i = 0; i < n; ++i) {
    cin >> num[i];
  }
  for (int i = 0; i < n - 2; ++i) {
    int lo = i + 1, hi = n - 1;
    while (lo < hi) {
      int mid = lo + (hi - lo + 1) / 2;
      if (check(num[mid], num[i]))
        lo = mid;
      else
        hi = mid - 1;
    }
    if (check(num[lo], num[i])) {
      maxdist[i] = lo;
    }
  }
  long long res = 0;
  for (int i = 0; i < n - 2; ++i) {
    if (maxdist[i] == 0) continue;
    int f = i, t = maxdist[i];
    res += comb(t - f);
  }
  cout << res << endl;
  return 0;
}
