#include <bits/stdc++.h>
using namespace std;
int binary_search_(vector<long long>& A, long long decrease, long long d,
                   int imin, int imax) {
  if (imax < imin)
    return imax;
  else {
    int imid = (imin + imax) / 2;
    if (A[imid] - decrease < d)
      return binary_search_(A, decrease, d, imid + 1, imax);
    else if (A[imid] - decrease == d)
      return imid;
    else
      return binary_search_(A, decrease, d, imin, imid - 1);
  }
}
int main() {
  long long N, d;
  cin >> N >> d;
  vector<long long> mas(N);
  for (int i = 0; i < N; i++) cin >> mas[i];
  long long sum = 0;
  for (int i = 0; i < N - 2; i++) {
    long long bs = binary_search_(mas, mas[i], d, i + 2, N - 1);
    sum += (bs - i) * (bs - i - 1) / 2;
  }
  cout << sum;
  return 0;
}
