#include <bits/stdc++.h>
using namespace std;
const int INF = 0x3f3f3f3f;
const long long LLINF = 0x3f3f3f3f3f3f3f3f;
const int MOD = 1000000007;
const double EPS = 1e-8;
const long double EULER = 2.71828182845904523536;
const long double PI = 3.14159265358979323846;
long long enciar2(int n) {
  if (n < 2) return 0LL;
  long long ans = n * (n - 1LL) / 2LL;
  return ans;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  long long n, d;
  while (cin >> n >> d) {
    long long arr[n];
    for (int i = 0; i < n; i++) cin >> arr[i];
    long long l = 0, r = 1;
    long long ans = 0;
    while (l < n && r < n) {
      if (arr[r] - arr[l] <= d) {
        ans += enciar2(r - l);
        r++;
      } else {
        l++;
        if (l == r) r++;
      }
    }
    cout << ans << endl;
  }
  return 0;
}
