#include <bits/stdc++.h>
using namespace std;
int dx[] = {1, 1, 0, -1, -1, -1, 0, 1};
int dy[] = {0, 1, 1, 1, 0, -1, -1, -1};
template <typename T>
void printV(vector<T> v) {
  for (int i = 0; i < v.size(); i++) {
    cout << v[i] << " ";
  }
  cout << endl;
}
long long nCr(long long n) {
  long long ans = (n * (n - 1) * (n - 2)) / 6;
  return ans;
}
void solve() {
  int n, d;
  cin >> n >> d;
  vector<int> v(n);
  for (int i = 0; i < n; i++) {
    cin >> v[i];
  }
  long long count = 0;
  int i = 0;
  while (i < n) {
    long long num = d + v[i];
    int idx = lower_bound((v).begin(), (v).end(), num) - v.begin();
    if (idx == n || v[idx] > num) {
      idx--;
    }
    long long between = idx - i - 1;
    count += (between) * (between + 1) / 2;
    i++;
  }
  cout << count << endl;
}
int main() {
  ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  int t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
