#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1), E = exp(1.0), EPS = DBL_EPSILON;
const long long int MOD = (long long int)1e9 + 7, N = (int)1e5;
int a[N + 1];
int BS(int n, int idx, int d) {
  int l = idx + 2, r = n - 1, res = -1;
  while (l <= r) {
    int mid = (l + r) / 2;
    if (a[mid] <= a[idx] + d) {
      res = mid;
      l = mid + 1;
    } else {
      r = mid - 1;
    }
  }
  return res;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, d, x;
  cin >> n >> d;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  long long int cnt = 0;
  for (int i = 0; i < n; i++) {
    int idx = BS(n, i, d);
    if (idx == -1) continue;
    x = idx - i - 1;
    long long int tmp = ((long long int)x * (x + 1)) / 2;
    cnt += tmp;
  }
  cout << cnt << "\n";
  return 0;
}
