#include <bits/stdc++.h>
using namespace std;
int main() {
  long long int n, d, x;
  cin >> n >> d;
  vector<long long int> v;
  for (int i = 0; i < n; i++) {
    cin >> x;
    v.push_back(x);
  }
  long long int c = 0;
  for (int i = 0, j = 0; i < n - 1; i++) {
    while (j < n && v[j] - v[i] <= d) j++;
    x = j - i - 1;
    if (x) c += (x * (x - 1)) / 2;
  }
  cout << c;
  return 0;
}
