#include <bits/stdc++.h>
using namespace std;
int MAX = 1000000;
int MIN = -1000000;
int INF = 1000000000;
int x4[4] = {0, 1, 0, -1};
int y4[4] = {1, 0, -1, 0};
int x8[8] = {0, 1, 1, 1, 0, -1, -1, -1};
int y8[8] = {1, 1, 0, -1, -1, -1, 0, 1};
int i, j, k;
int n, d, arr[100010];
int bin(int start, int A) {
  int L = start, R = n - 1, ret = -1;
  while (L <= R) {
    int mid = (L + R) / 2;
    if (arr[mid] <= A) {
      ret = max(ret, mid);
      L = mid + 1;
    } else
      R = mid - 1;
  }
  return ret;
}
int main() {
  cin >> n >> d;
  for (i = (0); i < (n); i++) cin >> arr[i];
  long long ans = 0ll;
  for (i = (0); i < (n); i++) {
    int now = arr[i];
    int patokan = now + d;
    int indx = bin(i, patokan);
    long long X = (indx - i);
    ans += ((X * (X - 1ll)) / 2ll);
  }
  cout << ans << endl;
  return 0;
}
