#include <bits/stdc++.h>
using namespace std;
vector<int> a;
long long sum;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, d;
  cin >> n >> d;
  a.resize(n);
  for (int i = 0; i < n; i++) cin >> a[i];
  for (int i = 0; i < n; i++) {
    int low = i + 1;
    int up = n;
    int index = -1;
    while (low <= up) {
      int mid = (low + up) / 2;
      if (abs(a[mid] - a[i]) <= d) {
        index = mid;
        low = mid + 1;
      } else if (abs(a[mid] - a[i]) > d)
        up = mid - 1;
      if (low == n) break;
    }
    if (index > -1) {
      int pb = (index - i) - 1;
      sum += 1ll * pb * (pb + 1) / 2;
    }
  }
  cout << sum;
  return 0;
}
