#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, m, a[100010], b[100010];
  long long x;
  scanf("%d%d", &n, &m);
  if (n <= 2)
    printf("0\n");
  else {
    for (int i = 0; i < n; i++) cin >> a[i];
    memset(b, 0, sizeof(b));
    long long sum = 0;
    int j = 0, i;
    for (i = 1; i < n; i++) {
      if (a[i] - a[j] <= m)
        b[j]++;
      else {
        j++;
        i--;
        if (b[j - 1] > 0) b[j] = b[j - 1] - 1;
      }
      if (i < j) i = j;
    }
    if (a[i] - a[j] <= m) j++;
    while (a[i] - a[j] <= m && j < i - 1 && b[j - 1] > 0) {
      b[j] = b[j - 1] - 1;
      j++;
    }
    x = n;
    if (j == 0)
      sum = (x - 2) * (x - 1) * x / 6;
    else {
      for (i = 0; i <= j; i++)
        if (b[i] >= 2) {
          x = b[i];
          sum += (x - 1) * x / 2;
        }
    }
    printf("%I64d\n", sum);
  }
}
