#include <bits/stdc++.h>
const long long inf = 1e9;
const long double eps = 1e-9;
const long long N = 1e6;
const long long M = 1e3;
using namespace std;
void ret(string s) {
  cout << s;
  exit(0);
}
int a[N];
bool u[N];
pair<int, int> p[N];
long long n, m, k, l, r, h, ans = 0, tmp = 0, d;
string s, s1;
int main() {
  cin >> n >> d;
  for (int i = 0; i < n; i++) cin >> a[i];
  a[n] = a[n - 1] + d + 2;
  for (int i = 0; i < n - 1; i++) {
    l = i;
    r = n;
    while (r - l > 1) {
      m = (r + l) / 2;
      if (a[m] - a[i] > d)
        r = m;
      else
        l = m;
    }
    if (l - i < 2) continue;
    h = l - i - 1;
    ans += ((h + 1) * h) / 2;
  }
  cout << ans;
  return 0;
}
