#include <bits/stdc++.h>
using namespace std;
long long int arr[100005];
int main() {
  ios::sync_with_stdio(0), ios_base::sync_with_stdio(0), cin.tie(0),
      cout.tie(0);
  ;
  long long int a, d, fans = 0;
  cin >> a >> d;
  for (int i = 0; i < a; ++i) cin >> arr[i];
  if (a < 3) return cout << 0 << "\n", 0;
  for (int i = 0; i <= a - 3; ++i) {
    long long int l = i + 2, r = a - 1, mid, ans = 0;
    while (l <= r) {
      mid = l + (r - l) / 2;
      if (arr[mid] - arr[i] < d)
        l = mid + 1, ans = mid;
      else if (arr[mid] - arr[i] == d) {
        ans = mid;
        break;
      } else
        r = mid - 1;
    }
    if (!ans) continue;
    long long int n;
    n = (ans - (i + 1));
    fans += ((n * (n + 1)) / 2);
  }
  cout << fans << "\n";
  return 0;
}
