#include <bits/stdc++.h>
using namespace std;
const long long int inf = 1e9 + 7;
const double pie = 3.14159265358979323846;
const char nl = '\n';
const char spc = ' ';
template <typename Type>
istream &operator>>(istream &in, vector<Type> &v) {
  long long int n = v.size();
  for (int i = 0; i < n; i++) in >> v[i];
  return in;
}
template <typename Type>
ostream &operator<<(ostream &out, vector<Type> &v) {
  for (auto value : v) out << value << ' ';
  out << nl;
  return out;
}
bool isPrime(long long int n) {
  if (n < 4) return true;
  if (n % 2 == 0) return false;
  long long int i = 3;
  while (i * i <= n) {
    if (n % i == 0) return false;
    i += 2;
  }
  return true;
}
void solve() {
  long long int n, d;
  cin >> n >> d;
  vector<long long int> a(n);
  cin >> a;
  long long int c = 0, l = 0;
  for (long long int r = 0; r < n; ++r) {
    while (a[r] - a[l] > d) ++l;
    long long int len = r - l;
    c += len * (len - 1) / 2;
  }
  cout << c << nl;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  long long int t;
  t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
