#include <bits/stdc++.h>
using namespace std;
const long long oo = 1e9;
const int N = 1e5 + 1, MOD = 1e8;
int n, d, t[N];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> d;
  for (int i = 0; i < n; i++) {
    cin >> t[i];
  }
  long long ans = 0;
  for (int i = 0; i < n; i++) {
    auto up = upper_bound(t + i, t + n, d + t[i]);
    int seg = up - t - i;
    if (seg > 2) {
      seg--;
      ans += (long long)seg * (seg - 1) / 2;
    }
  }
  cout << ans;
}
