#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, d;
  cin >> n >> d;
  vector<long long> arr(n);
  for (long long i = 0; i < n; i++) cin >> arr[i];
  vector<long long> index(n + 1);
  index[0] = 0;
  for (long long i = 1; i <= n; i++) {
    long long j = index[i - 1] + 1;
    for (j; j < n; j++) {
      if (arr[j] - arr[i - 1] > d) break;
    }
    index[i] = j - 1;
  }
  long long ans = 0;
  for (int i = 1; i <= n - 2; i++) {
    if (index[i] - (i - 1) >= 2) {
      index[i] = index[i] - i + 1;
      ans += index[i] * (index[i] - 1) / 2;
    }
  }
  cout << ans << '\n';
}
