#include <bits/stdc++.h>
using namespace std;
int n, d, a[100010];
int main() {
  scanf("%d%d", &n, &d);
  for (int i = 0; i < n; i++) scanf("%d", a + i);
  long long ans = 0;
  for (int i = 0, j = 0; i < n; i++) {
    while (j < n && a[j] - a[i] <= d) j++;
    ans += 1LL * (j - i - 1) * (j - i - 2) / 2;
  }
  cout << ans << endl;
  return 0;
}
