#include <bits/stdc++.h>
using namespace std;
long long int a[1000000];
int main() {
  long long int n, d;
  cin >> n >> d;
  for (long long int i = 0; i < n; i++) {
    cin >> a[i];
  }
  long long int ans = 0;
  for (long long int i = 0, j = 0; i < n; i++) {
    while (j < n && a[j] - a[i] <= d) {
      ans += ((j - i) * (j - i - 1)) / 2;
      j++;
    }
  }
  cout << ans;
  return 0;
}
