#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1);
const int MOD = 1e9 + 7;
const int MX = 2e5 + 5;
const long long INF = 1e18 + 5;
struct hash_pair {
  template <class T1, class T2>
  size_t operator()(const pair<T1, T2>& p) const {
    auto hash1 = hash<T1>{}(p.first);
    auto hash2 = hash<T2>{}(p.second);
    return hash1 ^ hash2;
  }
};
long long gcd(long long a, long long b) {
  while (b) {
    a %= b;
    swap(a, b);
  }
  return a;
}
long long lcm(long long a, long long b) { return (a / gcd(a, b) * b); }
long long comb(long long a, long long b) {
  long long x = max(a - b, b), ans = 1;
  for (long long K = a, L = 1; K >= x + 1; K--, L++) {
    ans *= K;
    ans /= L;
  }
  return ans;
}
void task() {
  int n, d, l = 1;
  long long ans = 0;
  cin >> n >> d;
  int ara[n];
  for (int K = 0; K < n; K++) cin >> ara[K];
  if (n <= 2) {
    cout << 0 << "\n";
    return;
  }
  for (int K = 0; K < n; K++) {
    for (int L = l + 1; L < n; L++) {
      if (ara[L] - ara[K] > d) break;
      l = L;
    }
    ans += (long long)(l - 1 - K);
    ans += (long long)(l - 1 - K - 1) * (l - 1 - K) / 2;
  }
  cout << ans << "\n";
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  task();
  return 0;
}
