#include <bits/stdc++.h>
using namespace std;
int main() {
  cin.tie(NULL);
  ios::sync_with_stdio(false);
  int n, d;
  cin >> n >> d;
  vector<int> arr(n);
  for (int i = 0; i < n; i++) cin >> arr[i];
  long long res = 0;
  for (int i = 0; i < n - 2; i++) {
    int start = i;
    int end = n - 1;
    int ans = -1;
    while (start <= end) {
      int mid = (start + end) / 2;
      if (arr[mid] - arr[i] <= d) {
        ans = mid;
        start = mid + 1;
      } else
        end = mid - 1;
    }
    if (ans - i >= 2) {
      res += ((long long)(ans - i) * (long long)(ans - i - 1)) / 2;
    }
  }
  cout << res << '\n';
  return 0;
}
