#include <bits/stdc++.h>
using namespace std;
bool is_prime(int n) {
  if (n == 2) return true;
  if (n % 2 == 0) return false;
  for (int i = 3; i <= sqrt(n); i += 2) {
    if (n % i == 0) return false;
  }
  return true;
}
int main() {
  long long res = 0;
  int n, d;
  cin >> n >> d;
  vector<long long> v(n + 10);
  for (int i = 0; i < n; i++) cin >> v[i];
  for (int i = n; i < n + 10; i++) v[i] = 1e18;
  for (int i = 0; i < n - 2; i++) {
    int x = lower_bound(v.begin(), v.end(), v[i] + d) - v.begin(), z = 0;
    if (v[x] > v[i] + d) z = -1;
    if (x - i + z >= 2) {
      long long y = x + z - i - 1;
      res += (y * (y + 1)) / 2;
    }
  }
  cout << res;
}
