#include <bits/stdc++.h>
using namespace std;
const int N = 100100;
long long a[N], n, d;
int main() {
  scanf("%I64d %I64d", &n, &d);
  for (int i = 1; i <= n; i++) cin >> a[i];
  if (n < 3) {
    printf("0\n");
    return 0;
  }
  long long ans = 0;
  for (long long i = 3; i <= n; i++) {
    long long k = lower_bound(a + 1, a + 1 + n, a[i] - d) - a;
    ans += (i - k) * (i - k - 1) / 2;
  }
  cout << ans;
}
