#include <bits/stdc++.h>
using namespace std;
int main() {
  long long n, d, arr[100005], i, pos, ans = 0;
  scanf("%I64d %I64d", &n, &d);
  for (i = 0; i < n; i++) scanf("%I64d", &arr[i]);
  arr[n] = 1e18;
  long long st = 0, nd = 0;
  while (st < n) {
    while (arr[nd] - arr[st] <= d) nd++;
    pos = nd - 1 - st;
    if (pos - 1 > 0) {
      ans = ans + (pos * (pos - 1)) / 2;
    }
    st++;
  }
  printf("%I64d\n", ans);
}
