#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  ;
  int n, d;
  long long int group = 0;
  cin >> n >> d;
  int arr[n + 10];
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  for (int i = 0; i < n - 2; i++) {
    int l = i + 1, r = n - 1, mid = 0;
    long long int res = 0;
    while (l <= r) {
      mid = l + (r - l) / 2;
      if (abs(arr[mid] - arr[i]) <= d) {
        res = mid;
        l = mid + 1;
      } else {
        r = mid - 1;
      }
    }
    if (res > 0) {
      group += ((res - i) * (res - i - 1)) / 2;
    }
  }
  cout << group << "\n";
  return 0;
}
