#include <bits/stdc++.h>
using namespace std;
long long arr[100001];
long long cnt[100001];
int main() {
  long long n, d, x, y;
  cin >> n >> d;
  for (int i = 0; i < n; i++) cin >> arr[i];
  x = n - 2;
  y = n - 1;
  while (x >= 0 && y >= 0) {
    if ((arr[y] - arr[x]) <= d) {
      cnt[x] += (y - x);
      x--;
    } else
      y--;
  }
  long long ans = 0;
  for (int i = 0; i < n - 1; i++) {
    if (cnt[i] > 1) ans += ((cnt[i] - 1) * (cnt[i]) / 2);
  }
  cout << ans << endl;
}
