#include <bits/stdc++.h>
using namespace std;
int main() {
  int N, K;
  cin >> N >> K;
  vector<int> a(300000 + 1);
  for (int i = 0; i < N; i++) {
    int A;
    cin >> A;
    a[A]++;
  }
  vector<long long> acc(300000 + 2);
  for (int i = 0; i < 300000 + 1; i++) {
    acc[i + 1] = acc[i] + a[i];
  }
  long long ans = 0;
  for (int i = 0; i < 300000 + 2 - 2 * K; i++) {
    ans = max(ans, acc[i + 2 * K] - acc[i]);
  }
  cout << ans << endl;
}
