#include <bits/stdc++.h>
using namespace std;
long long n, d, A[100001];
int main() {
  ios::sync_with_stdio(0);
  cin >> n >> d;
  for (long long i = 0; i < (long long)n; i++) cin >> A[i];
  long long Res = 0;
  for (int i = 0; i < n - 2; i++) {
    long long *p = upper_bound(A + i + 2, A + n, d + A[i]);
    if (p == A + i + 2) continue;
    p--;
    long long idx = p - A;
    long long D = idx - i;
    Res += (D * (D - 1)) / 2;
  }
  cout << Res << endl;
}
