#include <bits/stdc++.h>
using namespace std;
long long comb(long long size) {
  if (size < 2) return 0;
  return (size * (size - 1)) / 2;
}
int main() {
  (ios_base::sync_with_stdio(false), cin.tie(nullptr));
  int t;
  t = 1;
  while (t--) {
    long long n, d;
    cin >> n >> d;
    long long arr[n];
    for (int i = 0; i < n; i++) cin >> arr[i];
    long long count = 0;
    for (int i = 0; i < n - 2; i++) {
      long long search = arr[i] + d;
      long long index = lower_bound(arr, arr + n, search) - arr;
      if (index == n)
        count += comb(n - i - 1);
      else {
        if (arr[i] + d < arr[index]) index--;
        count += comb(index - i);
      }
    }
    cout << count;
  }
  return EXIT_SUCCESS;
}
