#include <bits/stdc++.h>
using namespace std;
const int LG = 20;
const int N = 1e5 + 10;
signed main() {
  int n, d;
  cin >> n >> d;
  int a[n + 4];
  for (int i = 1; i <= n; i++) cin >> a[i];
  long long ans = 0;
  for (int i = 1; i <= n - 2; i++) {
    auto it = upper_bound(a + i, a + n + 1, a[i] + d) - a;
    it--;
    int ct = it - i;
    if (it != i) ans += ct * (long long)(ct - 1) / 2;
  }
  cout << ans << '\n';
  return 0;
}
