#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, d;
  scanf("%d %d", &n, &d);
  int data[n + 10];
  for (int i = 0; i < n; i++) scanf("%d", &data[i]);
  long long ans = 0;
  for (int i = 0; i < n - 1; i++) {
    int h = upper_bound(data, data + n, data[i] + d) - data;
    h--;
    int l = i;
    int n = (h - l) + 1;
    if (n < 3) continue;
    n -= 2;
    ans += ((long long)(n) * (long long)(n + 1)) / 2;
  }
  cout << ans << endl;
  return 0;
}
