#include <bits/stdc++.h>
using namespace std;
long long nC3(int n) {
  if (n <= 3) return max(0, n / 3);
  return 1ll * n * (n - 1) * (n - 2) / 6;
}
int main() {
  int n, d;
  scanf("%d%d", &n, &d);
  int arr[n];
  for (int i = 0; i < n; i++) scanf("%d", &arr[i]);
  queue<int> que;
  long long ans = 0;
  for (int i = 0; i < n;) {
    if (que.empty()) {
      que.push(arr[i]);
      i++;
    } else {
      int x = que.front();
      if (arr[i] - x <= d) {
        que.push(arr[i]);
        i++;
      } else {
        int sz = que.size();
        ans += nC3(sz) - nC3(sz - 1);
        que.pop();
      }
    }
  }
  int sz = que.size();
  ans += nC3(sz);
  cout << ans << endl;
  return 0;
}
