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