#include <bits/stdc++.h>
using namespace std;
bool isvowel(char c) {
  if (c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u' || c == 'A' ||
      c == 'E' || c == 'I' || c == 'O' || c == 'U')
    return true;
  return false;
}
bool prime[1000001];
int spf[1000001];
vector<int> primes;
void sieve() {
  memset(prime, true, sizeof(prime));
  for (int p = 2; p * p <= 1000000; p++) {
    if (prime[p] == true) {
      for (int i = p * p; i <= 1000000; i += p) {
        prime[i] = false;
        spf[i] = p;
      }
    }
  }
  for (int p = 2; p <= 1000000; p++)
    if (prime[p]) primes.push_back(p);
}
vector<long long int> prime_factors(long long int n) {
  vector<long long int> pf;
  long long int sq = sqrt(n) + 1;
  for (long long int j = 2; j < sq + 1; j++) {
    if (n % j == 0) {
      pf.push_back(j);
      while (n % j == 0) n /= j;
    }
  }
  if (n > 2) pf.push_back(n);
  return pf;
}
long long int gcd(long long int a, long long int b) {
  if (b == 0) return abs(a);
  return gcd(b, a % b);
}
long long int fast_exp(long long int x, long long int n) {
  long long int ans = 1;
  while (n) {
    if (n & 1) ans *= x;
    n = n >> 1;
    x = x * x;
  }
  return ans;
}
long long int mod_exp(long long int x, long long int y, long long int p) {
  long long int res = 1;
  x = x % p;
  while (y > 0) {
    if (y & 1) res = (res * x) % p;
    y = y >> 1;
    x = (x * x) % p;
  }
  return res;
}
int main() {
  int n, d;
  cin >> n >> d;
  int a[n];
  for (long long int i = 0; i < n; i++) cin >> a[i];
  int p1 = 0, p2 = 1;
  long long int ans = 0, diff = 0, len = 0;
  while (p1 < n && p2 < n) {
    len = p2 - p1;
    if (len < 2) {
      p2++;
      continue;
    }
    diff = a[p2] - a[p1];
    if (diff <= d) {
      ans += len * (len - 1) / 2;
      p2++;
    } else
      p1++;
  }
  cout << ans;
}
