#include <bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
long long C(int n, int r) {
  if (r > n / 2) r = n - r;
  long long ans = 1;
  int i;
  for (i = 1; i <= r; ++i) {
    ans *= n - r + i;
    ans /= i;
  }
  return ans;
}
int n, d, a[100005];
int binarySearch(int idx) {
  int lo = idx, hi = n - 1, ret = 0;
  while (lo <= hi) {
    int mid = (lo + hi) / 2;
    if (a[mid] - a[idx] <= d) {
      ret = mid - idx;
      lo = mid + 1;
    } else {
      hi = mid - 1;
    }
  }
  return ret;
}
int main() {
  cin >> n >> d;
  for (int i = 0; i < n; ++i) cin >> a[i];
  long long res = 0;
  for (int i = 0; i < n; ++i) {
    int n = binarySearch(i) + 1;
    if (n >= 3) {
      res += C(n - 1, 2);
    }
  }
  cout << res << '\n';
  return 0;
}
