#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1e6 + 3;
const long long INF = 2e9;
const long long N = 1e6 + 2;
const long long MAX = 2e5 + 2;
const long double EPS = 1e-6;
long long a[N], b[N], ans[N];
long long cnt(long long x) {
  x--;
  if (x <= 1) return 0;
  return x * (x - 1) / 2;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  long long n, d;
  cin >> n >> d;
  for (long long i = 0; i < n; i++) cin >> a[i];
  long long cur = 0, ans = 0;
  for (long long i = 0; i < n; i++) {
    while (cur < n - 1 && a[cur + 1] - d <= a[i]) cur++;
    ans += cnt(cur - i + 1);
  }
  cout << ans;
}
