#include <bits/stdc++.h>
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int n, d;
  cin >> n >> d;
  vector<int> arr;
  for (long long i = 0; i < (n); ++i) {
    int temp;
    cin >> temp;
    arr.push_back(temp);
  }
  long long ans = 0;
  for (auto it = arr.begin(); it != arr.end(); ++it) {
    long long temp = (it - upper_bound(arr.begin(), arr.end(), *it - d - 1));
    ans += (long long)(temp * (temp - 1)) / 2LL;
  }
  cout << ans << '\n';
  return 0;
}
