#include <bits/stdc++.h>
using namespace std;
int main() {
  vector<int> vec;
  int n, k;
  scanf("%d%d", &n, &k);
  for (int x, i = 0; i < n; i++) {
    scanf("%d", &x);
    vec.push_back(x);
  }
  if (n < 3) {
    cout << 0;
    return 0;
  }
  long long res = 0;
  for (int i = 0; i < n - 2; i++) {
    auto it = upper_bound(vec.begin(), vec.end(), vec[i] + k) - 1;
    long long idx = it - vec.begin(), m = idx - i;
    if (idx <= i) continue;
    res += (m * (m - 1)) >> 1;
  }
  cout << res;
}
