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