#include <bits/stdc++.h>
using namespace std;
class Solution {
 public:
  int maxLen(int N, int K, const vector<int>& A) {
    int maxA = *max_element(A.begin(), A.end());
    vector<int> diff(maxA + 1, 0);
    for (int a : A) {
      diff[max(0, a - K)] += 1;
      diff[min(a + K + 1, maxA)] -= 1;
    }
    for (int i = 1; i < N; i++) diff[i] += diff[i - 1];
    int res = 1;
    for (int a : A) res = max(res, diff[a]);
    return res - 1;
  }
};
int main() {
  int N, K;
  scanf("%d%d", &N, &K);
  vector<int> A(N);
  for (int i = 0; i < N; i++) scanf("%d", &A[i]);
  printf("%d\n", Solution().maxLen(N, K, A));
  return 0;
}
