#include <bits/stdc++.h>
using namespace std;
long long gcd(long long a, long long b) { return (b == 0 ? a : gcd(b, a % b)); }
long long lcm(long long a, long long b) { return (a * (b / gcd(a, b))); }
long long bigmod(long long b, long long p, long long m) {
  if (p == 0) return 1;
  if (p % 2 == 0) {
    long long temp = bigmod(b, p / 2, m);
    return (temp * temp) % m;
  }
  return (bigmod(b, p - 1, m) * (b % m)) % m;
}
vector<vector<int> > v;
vector<bool> visited;
vector<int> top;
int visit[50];
void dfs(int node) {
  visited[node] = true;
  for (int i = 0; i < (int)v[node].size(); i++)
    if (!visited[v[node][i]]) dfs(v[node][i]);
  top.push_back(node);
}
int connectedcomponenetscnt() {
  int cnt = 0, n;
  for (int i = 1; i <= n; i++) {
    if (!visited[i]) {
      dfs(i);
      cnt++;
    }
  }
  return cnt;
}
long long solve(long long x) { return x * (x + 1) / 2; }
int main() {
  int n, m, x;
  long long sum = 0, pos;
  cin >> n >> m;
  vector<int> a;
  for (int i = 0; i < n; i++) {
    cin >> x;
    a.push_back(x);
  }
  sort(a.begin(), a.end());
  for (int i = 0; i < n; i++) {
    pos = upper_bound(a.begin(), a.end(), a[i] + m) - a.begin();
    if (pos - i >= 3) sum += solve(pos - i - 2);
  }
  cout << sum;
}
