#include <bits/stdc++.h>
const int maxn = 1e5 + 5;
long long a[maxn];
using namespace std;
int main() {
  long long n, d, sum = 0;
  cin >> n >> d;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
  }
  for (int i = 1; i <= n - 2; ++i) {
    long long t = upper_bound(a + 1, a + 1 + n, a[i] + d) - i - 1 - a;
    sum += t * (t - 1) / 2;
  }
  cout << sum;
}
