#include <bits/stdc++.h>
using namespace std;
int num[100005];
int main() {
  int n, d;
  scanf("%d%d", &n, &d);
  for (int i = 0; i < n; i++) {
    scanf("%d", num + i);
  }
  int p1 = 0, p2 = 2;
  long long sum = 0;
  while (p1 < n - 2) {
    while (p2 < n && num[p2] - num[p1] <= d) p2++;
    sum += (p2 - p1 - 2ll) * (p2 - p1 - 1) / 2;
    p1++;
  }
  printf("%I64d\n", sum);
  return 0;
}
