#include <bits/stdc++.h>
using namespace std;
long long bs(vector<long long> &v, long long i, long long l, long long r,
             long long d) {
  if (abs(v[r] - v[i]) <= d) return r;
  while (l <= r) {
    long long mid = (l + r) / 2;
    long long dis = abs(v[i] - v[mid]);
    if (dis <= d && abs(v[i] - v[mid + 1]) > d) {
      return mid;
    } else if (dis > d)
      r = mid - 1;
    else
      l = mid + 1;
  }
  return -1;
}
long long findGroups(vector<long long> &v, long long d) {
  long long i = 0, n = v.size() - 1, ans = 0;
  while (i <= n - 2) {
    long long k = bs(v, i, i + 2, n, d);
    if (k != -1) {
      long long dis = k - i;
      long long comb = dis * (dis - 1) / 2;
      ans += comb;
    }
    i++;
  }
  return ans;
}
signed main() {
  long long n, d;
  cin >> n >> d;
  vector<long long> v(n);
  for (long long i = 0; i < n; i++) {
    cin >> v[i];
  }
  cout << findGroups(v, d);
}
