#include <bits/stdc++.h>
using namespace std;
int arr[100002];
int n, d;
long long ans, cnt;
int binSearch(int target, int low, int high) {
  int mid = 0;
  while (low <= high) {
    mid = (low + high) / 2;
    if (target == arr[mid])
      return mid;
    else if (target < arr[mid])
      high = mid - 1;
    else
      low = mid + 1;
  }
  return high;
}
long long nC2(long long n) {
  long long ret = 0;
  if (n % 2 == 0) {
    ret = n / 2;
    ret = ret * (n - 1);
  } else {
    ret = (n - 1) / 2;
    ret = ret * n;
  }
  return ret;
}
int main() {
  int s;
  scanf("%d", &n);
  scanf("%d", &d);
  for (int i = 0; i < (int)n; i++) scanf("%d", &arr[i]);
  for (int i = 0; i < n; i++) {
    cnt = (long long)binSearch(arr[i] + d, i, n - 1);
    cnt = cnt - i;
    if (cnt < 2) continue;
    cnt = nC2(cnt);
    ans += cnt;
  }
  cout << ans << endl;
  return 0;
}
