#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long int i, j, n, d, c = 0;
  cin >> n >> d;
  vector<long long int> a(n);
  for (i = 0; i < n; i++) cin >> a[i];
  for (i = 0; i < n - 2; i++) {
    long long int it = lower_bound(a.begin(), a.end(), a[i] + d) - a.begin();
    if (it > n - 1) it = n - 1;
    if (a[it] - a[i] > d) it--;
    long long int x = it - i - 1;
    c += x * (x + 1) / 2;
  }
  cout << c;
  return 0;
}
