#include <bits/stdc++.h>
using namespace std;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  long long N, d;
  cin >> N >> d;
  vector<long long> arr(N);
  for (long long i = 0; i < N; i++) {
    cin >> arr[i];
  }
  long long i = 0, j = 1, k = 2;
  long long cnt = 0;
  while (k < N) {
    if (arr[k] - arr[i] >= d) {
      if (arr[k] - arr[i] == d) {
        cnt += (k - i - 1);
      }
      i++;
      if (i == j) {
        j++;
      }
      if (j == k) {
        k++;
      }
    } else {
      cnt += (long long)(k - i - 1) * (k - i) / 2;
      k++;
    }
  }
  cout << cnt << endl;
}
