#include <bits/stdc++.h>
using namespace std;
const long long int N = 1e5 + 5;
const long long int mod = 1e9 + 9;
bool not_prime[N];
long long int fast_exp(long long int a, long long int b) {
  if (a == 0) return 0;
  if (b == 0) return 1;
  if (b == 1) return a;
  if (b % 2 == 0) {
    return fast_exp(((a % mod) * (a % mod)) % mod, b / 2) % mod;
  }
  return ((a % mod) * fast_exp(((a % mod) * (a % mod)) % mod, (b - 1) / 2)) %
         mod;
}
long long int gcd(long long int a, long long int b) {
  while (a != 0 && b != 0) {
    if (a > b)
      a = a % b;
    else
      b = b % a;
  }
  return (a == 0) ? b : a;
}
void primes() {
  long long int value = 1e6;
  long long int sq_value = sqrt(value);
  for (long long int i = 2; i <= sq_value; i++) {
    if (!not_prime[i]) {
      for (long long int j = i * i; j <= value; j += i) {
        not_prime[j] = true;
      }
    }
  }
}
long long int n, d;
long long int a[N];
long long int search(long long int low, long long int temp) {
  long long int ans = -1;
  long long int high = n;
  while (low <= high) {
    long long int mid = (low + high) / 2;
    if (a[mid] > temp) {
      high = mid - 1;
    } else if (a[mid] < temp) {
      ans = mid;
      low = mid + 1;
    } else {
      ans = mid;
      break;
    }
  }
  return ans;
}
long long int calc(long long int x) { return x * (x - 1) / 2; }
int main() {
  ios::sync_with_stdio(false);
  long long int temp, index, ans = 0;
  cin >> n >> d;
  for (long long int i = 1; i < n + 1; i++) {
    cin >> a[i];
  }
  for (long long int i = 1; i < n - 1; i++) {
    temp = a[i] + d;
    index = search(i, temp);
    if (index != -1) {
      temp = index - i;
      ans += calc(temp);
    }
  }
  cout << ans << endl;
  return 0;
}
