#include <bits/stdc++.h>
using namespace std;
int binsort(int *array, int o, int p, int num) {
  int mid;
  if (array[o] > num) {
    return o - 2;
  }
  if (array[p] <= num) {
    return p;
  }
  while (o != p) {
    mid = (o + p) / 2 + 1;
    if (array[mid] == num) {
      return mid;
    } else if (array[mid] < num) {
      o = mid;
    } else {
      p = mid - 1;
    }
  }
  return o;
}
int main() {
  int n, d;
  long long arrayc[100005] = {0, 0};
  cin >> n >> d;
  int *array = new int[n];
  int *array2 = new int[n];
  for (int i = 0; i < n; i++) {
    scanf("%d", &array[i]);
  }
  long long result = 0;
  for (int i = 0; i < n - 2; i++) {
    array2[i] = binsort(array, i + 2, n - 1, array[i] + d);
  }
  for (int i = 0; i < n - 2; i++) {
    long long j = array2[i] - i;
    if (j >= 2) {
      if (arrayc[j] == 0) {
        arrayc[j] = j * (j - 1) / 2;
      }
      result += arrayc[j];
    }
  }
  cout << result << endl;
  return 0;
}
