#include <bits/stdc++.h>
int main() {
  long long n, d, dist;
  scanf("%I64d %I64d", &n, &d);
  long long arr[n];
  for (long long i = 0; i < n; i++) {
    scanf("%I64d", &arr[i]);
  }
  if (n < 3) {
    printf("0\n");
    return 0;
  }
  long long cnt = 0, j = 2, cn = 0;
  for (long long i = 0; i < n - 2; i++) {
    while (j < n) {
      dist = arr[j] - arr[i];
      if (dist <= d)
        cn++;
      else
        break;
      j++;
    }
    cnt += (cn * (cn + 1)) / 2;
    cn -= 1;
  }
  printf("%I64d\n", cnt);
  return 0;
}
