#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int n, d;
  cin >> n >> d;
  vector<long long int> Array(n);
  for (int i = 0; i < n; i++) {
    cin >> Array[i];
  }
  long long int ans = 0;
  int r, l;
  l = 0, r = 0;
  while (l < n - 2) {
    while (r < n - 1 && Array[r + 1] - Array[l] <= d) r++;
    ans += (r - l) * 1ll * (r - l - 1) / 2;
    l++;
  }
  cout << ans;
  return 0;
}
