#include <bits/stdc++.h>
using namespace std;
int a[100010];
inline long long C(int n, int m) {
  if (n < m) return 0;
  m = n - m < m ? n - m : m;
  long long res = 1;
  for (int i = 1; i <= m; i++) res = res * (n - i + 1) / i;
  return res;
}
int main(int argc, const char* argv[]) {
  int n, d;
  scanf("%d%d", &n, &d);
  for (int i = 0; i < n; i++) scanf("%d", &a[i]);
  long long ans = 0;
  for (int i = n - 1; i >= 2; i--) {
    int x = a[i] - d, p = lower_bound(a, a + n, x) - a, c = i - p;
    ans += C(c, 2);
  }
  cout << ans << "\n";
  return 0;
}
