#include <bits/stdc++.h>
using namespace std;
long long mod = 1e9 + 7;
double eps = 1e-9;
const int MAX_SIZE = 1e7 + 1;
int n, t, m, q, k, p;
long long int a[1 << 17];
long long int d;
int main() {
  ios_base::sync_with_stdio(0);
  cin >> n >> d;
  for (int i = 0; i < n; ++i) cin >> a[i];
  long long int ans = 0ll;
  int left = 0, right = 0;
  while (right < n) {
    if (right - left + 1 < 3)
      right++;
    else if (a[right] - a[left] > d)
      left++;
    else {
      long long int size = (right - left - 1);
      ans += size * (size + 1ll) / 2ll;
      right++;
    }
  }
  cout << ans << '\n';
  cin >> n;
}
