#include <bits/stdc++.h>
using namespace std;
string star[200000], str, str1;
long long ara[100005], ara2[200005];
vector<pair<long long, long long> > ab;
vector<long long> x;
vector<long long> y;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  long long int n, ans, temp, t, j, i, m, h, h2, m2, c, a, b, mx, d, k, l, r;
  cin >> n >> d;
  for (i = 0; i < n; i++) {
    cin >> a;
    x.push_back(a);
  }
  ans = 0;
  for (i = 0; i < n - 2; i++) {
    j = upper_bound(x.begin(), x.end(), (x[i] + d)) - x.begin();
    a = j - i - 1;
    ans += (a * (a - 1)) / 2;
  }
  cout << ans;
  return 0;
}
