#include <bits/stdc++.h>
using namespace std;
int a[100005];
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  long long int i, j, k, l, m, n, d, v = 0;
  unsigned long long int ct = 0;
  cin >> n >> d;
  for (i = 0; i < n; i++) cin >> a[i];
  if (n < 3) {
    cout << 0 << endl;
    return 0;
  }
  if (n == 3) {
    if (abs(a[0] - a[2]) > d) {
      cout << 0 << endl;
    } else
      cout << 1 << endl;
    return 0;
  }
  for (i = 0; i <= n - 2; i++) {
    k = a[i + 2];
    {
      v = upper_bound(a, a + n, a[i] + d) - a;
      v--;
      int dx = v - i;
      if (v - i >= 2) ct += (long long int)dx * (long long int)(dx - 1) / 2;
    }
  }
  cout << ct << endl;
  return 0;
}
