#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, d;
  cin >> n >> d;
  long long sum = 0;
  int arr[n];
  for (int i = 0; i < n; i++) cin >> arr[i];
  for (int i = 0; i < n; i++) {
    int low = i + 1, high = n - 1, mid, ans = i;
    while (low <= high) {
      mid = low + (high - low) / 2;
      if (arr[mid] - arr[i] > d)
        high = mid - 1;
      else {
        low = mid + 1;
        ans = mid;
      }
    }
    long long num = ans - i;
    sum += num * (num - 1) / 2;
  }
  cout << sum;
  return 0;
}
