#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define loop(i,l,r) for(int i = (l); i < (r); i++)
#define rloop(i,r,l) for(int i = (r-1); i >= (l); i--)
#define refloop(x, v) for(auto& x : (v))
#define exists(m,e) if((m).find(e) != (m).end())
#define nexists(m,e) if((m).find(e) == (m).end())
#define sortaka(v) sort((v).begin(), (v).end())
#define rsort(v) sort((v).rbegin(), (v).rend())
#define lower_bound_index(v, x) (lower_bound((v).begin(), (v).end(), x)-(v).begin()) // returns minimum i s.t v[i] >= x
#define upper_bound_index(v, x) (upper_bound((v).begin(), (v).end(), x)-(v).begin()) // returns minimum i s.t v[i] > x
#define scanint(x) scanf("%d", &(x))
#define scanfloat(x) scanf("%f", &(x))
#define scanll(x) scanf("%lld", &(x))
#define scanchar(x) scanf("%c", &(x))
#define scanstring(x) scanf("%s", &(x))
#define printint(x) printf("%d", x)
#define printfloat(x) printf("%.9f", x)
#define printll(x) printf("%lld", x)
#define printchar(x) printf("%c", x)
#define printstring(x) printf("%s", x)
#define printintnewline(x) printf("%d\n", x)
#define printfloatnewline(x) printf("%.9f\n", x)
#define printllnewline(x) printf("%lld\n", x)
#define printcharnewline(x) printf("%c\n", x)
#define printstringnewline(x) printf("%s\n", x)
#define square(x) ((x)*(x))
#define cube(x) ((x)*(x)*(x))

typedef vector<int> vi;
typedef vector<long long> vl;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef vector<pair<int, int>> vpii;
typedef vector<pair<long long, long long>> vpll;
typedef vector<vector<int>> vvi;
typedef vector<vector<long long>> vvl;
typedef vector<string> vs;
typedef vector<vector<pair<int, int>>> vvpii;
typedef vector<vector<pair<long long, long long>>> vvpll;
typedef map<int,int> mii;
typedef map<string,int> msi;
typedef long long ll;

void update(int ind, int val, vi &segtree){
    while(segtree[ind] < val && ind >= 0){
        segtree[ind] = val;
        ind = (ind-1)/2;
    }
    // refloop(x,segtree){
    //     cout<<x<<" ";
    // }
    // cout<<endl;
}

bool contained(pii p1, pii p2){
    if(p1.first >= p2.first && p1.second <= p2.second){
        return true;
    }else{
        return false;
    }
}

bool disjoint(pii p1, pii p2){
    if(p1.first > p2.second || p1.second < p2.first){
        // disjoint
        return true;
    }else{
        return false;
    }
}

int get(int l, int r, int ind, vi &segtree, vpii &span){

    if(contained(span[ind], {l,r})){
        // cout<<"conti \n";
        return segtree[ind];
    }else if(disjoint(span[ind], {l,r})){
        // cout << "disjoint \n";
        return 0;
    }else{
        // cout<<"recurse \n";
        return max(get(l, r, 2*ind+1, segtree, span), get(l, r, 2*ind+2, segtree, span));
    }
}

int main(){
    ios::sync_with_stdio(0);

    const int MAX = 524288;

    int n, k; cin >> n >> k;
    vi v(n);
    loop(i, 0, n){
        cin >> v[i];
    }

    vi segtree(2*MAX-1, 0);
    vpii span(2*MAX-1);

    rloop(i, 2*MAX-1, MAX-1){
        span[i] = {i-MAX+1, i-MAX+1};
    }
    rloop(i, MAX-1, 0){
        span[i] = {span[2*i+1].first, span[2*i+2].second};
    }

    rloop(i, n, 0){
        int le = max(0, v[i]-k);
        int re = min(MAX, v[i]+k);
        // cout << le << " " << re <<endl;
        int val = 1+get(le, re, 0, segtree, span);
        // cout << val << endl;
        update(MAX -1 + v[i], val, segtree);
    }

    int ans = 0;
    rloop(i, 2*MAX-1, MAX-1){
        ans = max(ans, segtree[i]);
    }

    // refloop(x,segtree){
    //     cout<<x<<" ";
    // }
    // cout<<endl;

    cout<<ans<<endl;
}

Measured memory_bytes: 16684