#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cout << setprecision(0) << fixed;
  long long n, d;
  cin >> n >> d;
  vector<long long> v(n);
  for (long long& i : v) cin >> i;
  long long sum = 0;
  auto it1 = (v).begin();
  for (long long i = 0; i < n - 2; i++) {
    auto it2 = lower_bound(it1, (v).end(), v[i] + d);
    long long x = (*it2 == v[i] + d ? it2 - it1 - 1 : it2 - it1 - 2);
    sum += x * (x + 1) / 2;
    it1++;
  }
  cout << sum << "\n";
}
