#include <bits/stdc++.h>
using namespace std;
int bisearch(vector<long long> &in, long long d, int i) {
  int r = -1;
  int beg = i;
  int end = in.size() - 1;
  while (beg <= end) {
    int mid = (beg + end) / 2;
    if (in[mid] - in[i] > d) {
      end = mid - 1;
    } else {
      r = mid;
      beg = mid + 1;
    }
  }
  return r;
}
int main() {
  int n = 5;
  long long d = 19;
  cin >> n >> d;
  vector<long long> in(n);
  for (int i = 0; i < n; i++) {
    cin >> in[i];
  }
  long long result = 0;
  for (int i = 0; i < in.size(); i++) {
    long long high = bisearch(in, d, i);
    if (high - i >= 2) {
      result += ((high - i) * (high - i - 1)) / 2;
    }
  }
  cout << result;
}
