#include <bits/stdc++.h>
using namespace std;
const long long inf = (long long)2 * 1e9;
const long long MOD = (long long)1e9 + 7;
const long long MAX_N = 100000;
const long double pi = 3.1415926;
const long double eps = 1e-6;
long long a[MAX_N];
long long bin_search(long long x, long long* a, long long n) {
  long long l = 0, r = n, mid;
  while (r - l > 1) {
    mid = (l + r) / 2;
    if (x >= a[mid])
      l = mid;
    else
      r = mid;
  }
  return l;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  long long n, d;
  cin >> n >> d;
  for (long long i = 0; i < n; i++) cin >> a[i];
  long long ans = 0;
  for (long long i = 0; i < n; i++) {
    long long j = bin_search(a[i] + d, a, n);
    long long cnt = j - i;
    ans += cnt * (cnt - 1) / 2;
  }
  cout << ans;
  return 0;
}
