#include <bits/stdc++.h>
using namespace std;
const int Max = 200001;
int main() {
  long long int n, d, ans = 0, x[Max], diff;
  cin >> n >> d;
  for (int i = 0; i < n; i++) {
    cin >> x[i];
  }
  int rightmost = 0;
  for (int i = 0; i < (n - 2); i++) {
    while (rightmost < n && x[rightmost] - x[i] <= d) {
      rightmost++;
    }
    diff = rightmost - i;
    if (diff > 2) {
      diff -= 2;
      ans += (diff * (diff + 1)) / 2;
    }
  }
  cout << ans;
}
