#include <bits/stdc++.h>
using namespace std;
long long n, m;
const long long N = 3e5 + 10;
long long a[N];
long long f[N];
long long bs1(long long p, long long k) {
  long long l = p, r = n, mid;
  long long ans = n;
  while (l < r) {
    mid = (l + r) >> 1;
    if (a[mid] - a[p] >= k) {
      r = mid - 1;
      ans = r;
    } else {
      l = mid + 1;
    }
  }
  return ans;
}
long long bs2(long long p, long long k) {
  if (p == 1) return p;
  long long l = 1, r = p, mid;
  long long ans = p;
  while (l < r) {
    mid = (l + r) >> 1;
    if (a[mid] - a[p] >= k) {
      r = mid - 1;
      ans = r;
    } else {
      l = mid + 1;
    }
  }
  return ans;
}
signed main() {
  cin >> n >> m;
  for (long long i = 1; i <= n; i++) cin >> a[i];
  sort(a + 1, a + n + 1);
  a[n + 1] = 0x7ffffffff;
  a[0] = 0x7ffffffff;
  long long ans = 0;
  for (long long i = 1; i <= n; i++) {
    long long b1 = bs1(i, m);
    long long b2 = bs2(i, m);
    while (a[b1] - a[i] <= m && b1 <= n) b1++;
    while (a[i] - a[b2] <= m && b2 >= 1) b2--;
    b1--;
    b2++;
    ans = max(ans, b1 - b2);
  }
  cout << ans << endl;
}
