#include <bits/stdc++.h>
using namespace std;
long long int n, d, arr[100005];
long long int bs(long long int x) {
  long long int l = 0, h = n - 1, mid, ans = -1;
  while (l < h) {
    mid = (l + h + 1) / 2;
    if (arr[mid] <= x + d) {
      l = mid;
      ans = mid;
    } else
      h = mid - 1;
  }
  return ans;
}
int main() {
  cin >> n >> d;
  for (int i = 0; i < n; i++) cin >> arr[i];
  if (n < 3)
    cout << "0" << endl;
  else {
    long long int f_ans = 0;
    for (long long int i = 0; i < n; i++) {
      long long int ans = bs(arr[i]);
      if (ans - i > 1) f_ans += (ans - i - 1) * (ans - i) / 2;
    }
    cout << f_ans << endl;
  }
}
