#include <bits/stdc++.h>
using namespace std;
long long int a[100000];
long long int n;
int bin(long long int x) {
  int end = n - 1;
  int start = 0;
  int t;
  while (end >= start) {
    int mid = (end + start) / 2;
    if (x == a[mid]) {
      return mid;
    } else if (x < a[mid]) {
      end = mid - 1;
      continue;
    } else if (x > a[mid]) {
      start = mid + 1;
      t = mid;
      continue;
    }
  }
  return t;
}
int main() {
  cin >> n;
  long long int d;
  cin >> d;
  long long int s = 0;
  for (int i = 0; i < n; i++) cin >> a[i];
  for (int i = 0; i < n; i++) {
    if (a[i] + d >= a[n - 1]) {
      if (n - 1 - i + 1 >= 3) {
        s += (n - 1 - i - 1 + 1) * (n - 1 - i - 1) / 2;
      }
    } else {
      long long int j = bin(d + a[i]);
      if (j - i + 1 >= 3) {
        s += (j - i - 1 + 1) * (j - i - 1) / 2;
      }
    }
  }
  cout << s;
}
