#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  long long d;
  cin >> n >> d;
  long long temp = n;
  vector<long long> arr(n);
  while (temp > 0) {
    cin >> arr[n - temp];
    --temp;
  }
  long long count = 0;
  for (int i = 0; i < n; ++i) {
    int l = i + 1, r = n - 1;
    while (l <= r) {
      int mid = (l + r) / 2;
      if (arr[mid] - arr[i] <= d) {
        l = mid + 1;
        continue;
      } else {
        r = mid - 1;
      }
    }
    count += (l - i - 1) * ((l - i - 2) / 2.0);
  }
  cout << count << endl;
}
