#include <bits/stdc++.h>
using namespace std;
int const NMAX = 5e4;
int n, k;
int a[1 + NMAX];
int le[1 + NMAX], ri[1 + NMAX];
int maxL[1 + NMAX], maxR[1 + NMAX];
int main() {
cin >> n >> k;
for(int i = 1; i <= n; i++) {
cin >> a[i];
}
sort(a+1, a+n+1);
int to = 1;
for(int i = 1; i <= n; i++) {
while(to < n && a[to+1]-a[i] <= k) {
to++;
}
ri[i] = to;
}
int from = n;
for(int i = n; i >= 1; i--) {
while(from > 1 && a[i]-a[from-1] <= k) {
from--;
}
le[i] = from;
}
for(int i = 1; i <= n; i++) {
maxL[i] = max(maxL[i-1], i-le[i]+1);
}
for(int i = n; i >= 1; i--) {
maxR[i] = max(maxR[i+1], ri[i]-i+1);
}
int ans = 0;
for(int i = 1; i < n; i++) {
ans = max(ans, maxL[i] + maxR[i+1]);
//cout << i << ": " << le[i] << ", " << ri[i] << "\n";
}
cout << ans << "\n";
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+Cgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKaW50IGNvbnN0IE5NQVggPSA1ZTQ7CmludCBuLCBrOwppbnQgYVsxICsgTk1BWF07CmludCBsZVsxICsgTk1BWF0sIHJpWzEgKyBOTUFYXTsKaW50IG1heExbMSArIE5NQVhdLCBtYXhSWzEgKyBOTUFYXTsKCmludCBtYWluKCkgewogICAgY2luID4+IG4gPj4gazsKICAgIGZvcihpbnQgaSA9IDE7IGkgPD0gbjsgaSsrKSB7CiAgICAgICAgY2luID4+IGFbaV07CiAgICB9CiAgICBzb3J0KGErMSwgYStuKzEpOwogICAgaW50IHRvID0gMTsKICAgIGZvcihpbnQgaSA9IDE7IGkgPD0gbjsgaSsrKSB7CiAgICAgICAgd2hpbGUodG8gPCBuICYmIGFbdG8rMV0tYVtpXSA8PSBrKSB7CiAgICAgICAgICAgIHRvKys7CiAgICAgICAgfQogICAgICAgIHJpW2ldID0gdG87CiAgICB9CiAgICBpbnQgZnJvbSA9IG47CiAgICBmb3IoaW50IGkgPSBuOyBpID49IDE7IGktLSkgewogICAgICAgIHdoaWxlKGZyb20gPiAxICYmIGFbaV0tYVtmcm9tLTFdIDw9IGspIHsKICAgICAgICAgICAgZnJvbS0tOwogICAgICAgIH0KICAgICAgICBsZVtpXSA9IGZyb207CiAgICB9CiAgICBmb3IoaW50IGkgPSAxOyBpIDw9IG47IGkrKykgewogICAgICAgIG1heExbaV0gPSBtYXgobWF4TFtpLTFdLCBpLWxlW2ldKzEpOwogICAgfQogICAgZm9yKGludCBpID0gbjsgaSA+PSAxOyBpLS0pIHsKICAgICAgICBtYXhSW2ldID0gbWF4KG1heFJbaSsxXSwgcmlbaV0taSsxKTsKICAgIH0KICAgIGludCBhbnMgPSAwOwogICAgZm9yKGludCBpID0gMTsgaSA8IG47IGkrKykgewogICAgICAgIGFucyA9IG1heChhbnMsIG1heExbaV0gKyBtYXhSW2krMV0pOwogICAgICAgIC8vY291dCA8PCBpIDw8ICI6ICIgPDwgbGVbaV0gPDwgIiwgIiA8PCByaVtpXSA8PCAiXG4iOwogICAgfQogICAgY291dCA8PCBhbnMgPDwgIlxuIjsKfQ==