#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f, maxn = 100001;
int n, d, a[maxn];
long long c(int n) { return (long long)n * (n - 1) / 2; }
int main() {
  while (~scanf("%d%d", &n, &d)) {
    for (int i = 0; i < n; i++) scanf("%d", &a[i]);
    long long ans = 0;
    for (int i = 0; i < n; i++) {
      int pos = upper_bound(a, a + n, a[i] + d) - a - 1;
      ans += c(pos - i);
    }
    printf("%lld\n", ans);
  }
  return 0;
}
