#include <bits/stdc++.h>
using namespace std;
const int oo = 100010;
long long n, d, x, ans;
vector<long long> v;
int main() {
  cin >> n >> d;
  for (int i = 0; i < n; i++) cin >> x, v.push_back(x);
  for (int i = 0; i < n - 2; i++) {
    vector<long long>::iterator it;
    it = upper_bound(v.begin(), v.end(), v[i] + d);
    x = ((it - v.begin()) - i - 2);
    ans += x * (x + 1) / 2;
  }
  cout << ans;
  return 0;
}
