#include <bits/stdc++.h> 
using namespace std; 
#define ll long long int
#define pb push_back             
#define se second                
#define fi first  
#define be begin()
#define en end()
#define all(v) v.be, v.en
template<typename... T> void pp(T... args) { ((cout << args << " "), ...); cout<<"\n"; }
template<typename... T> void po(T... args) { ((cout << args << " "), ...); cout<<""; }

// int longest_subseq(int n, int k, string s) 
// { 
  
//     // Creating a list with 
//     // all 0's of size 
//     // equal to the length of string 
//     vector<int> dp(n, 0); 
  
//     // Supporting list with 
//     // all 0's of size 26 since 
//     // the given string consists 
//     // of only lower case alphabets 
//     ll N = 3e5 + 5;
//     int max_length[N] = {0}; 
  
//     for (int i = 0; i < n; i++)  
//     { 
  
//         // Converting the ascii value to 
//         // list indices 
//         int curr = s[i] - 'a'; 
          
//         // Determining the lower bound 
//         int lower = max(0, curr - k); 
          
//         // Determining the upper bound 
//         int upper = min(N-1, curr + k); 
          
//         // Filling the dp array with values 
//         for (int j = lower; j < upper + 1; j++) 
//         { 
//             dp[i] = max(dp[i], max_length[j] + 1); 
//         } 
//         //Filling the max_length array with max 
//         //length of subsequence till now 
//         max_length[curr] = max(dp[i], max_length[curr]); 
//     } 
  
//     int ans = 0; 
  
//     for(int i:dp) ans = max(i, ans); 
  
//     // return the max length of subsequence 
//     return ans; 
// } 
 int arr[300005],tree[900005];
 void BuildTree(int start, int end, int treeNode){

     if(start==end){
         tree[treeNode]=arr[start];
         return ;
     }
     int mid=(start+end)/2;
     BuildTree(mid+1,end,2*treeNode+1);
     BuildTree(start,mid,2*treeNode);
     tree[treeNode]=max(tree[2*treeNode],tree[2*treeNode+1]);

 }

 void Update( int start, int end, int treeNode, int idx, int value){

     if(start==end){
         tree[treeNode]=value;
         arr[idx]=value;
         return;
     }
     int mid=(start+end)/2;
     if(idx>mid){
         Update(mid+1,end,2*treeNode+1,idx,value);
     } else {
         Update(start,mid,2*treeNode,idx,value);
     }
     tree[treeNode]=max(tree[2*treeNode+1],tree[2*treeNode]);
 }

 int Query(int start, int end, int treeNode, int left, int right){
     int mid=(start+end)/2;

    if(end<left || start>right)return 0;
    if(start>=left && end<=right){
        return tree[treeNode];
        //cout<<"YES";
    }
    int ans1=Query(start,mid,2*treeNode,left,right);
    int ans2=Query(mid+1,end,2*treeNode+1,left,right);
    //cout<<ans1<<" "<<ans2<<endl;
    return max(ans1,ans2);


 }


int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
 
     int test=1;
     //cin>>test;
     for(int tt=1; tt<=test; tt++){
       ll n,k;
       cin>>n>>k;
       ll a[n];
       for(ll i=0; i<n; i++){
           cin>>a[i];
       }

       vector<int> dp(n, 0); 
  
    // Supporting list with 
    // all 0's of size 26 since 
    // the given string consists 
    // of only lower case alphabets 
    ll N = 3e5 + 5;
    int arr[N+1] = {0}; 
     BuildTree(1,n,1);
    for (int i = 0; i < n; i++)  
    { 
  
        // Converting the ascii value to 
        // list indices 
        ll curr = a[i] ; 
          
        // Determining the lower bound 
        ll lower = max(0LL, curr - k); 
          
        // Determining the upper bound 
        ll upper = min(N-1, curr + k); 
         int ans1=Query(1,N,1,lower,upper);
        // Filling the dp array with values 
        // for (ll j = lower; j < upper + 1; j++) 
        // { 
        //     dp[i] = max(dp[i], arr[j] + 1); 
        // } 
        dp[i]=max(dp[i],(ans1)+1);
        //Filling the arr array with max 
        //length of subsequence till now 
        arr[curr] = max(dp[i], arr[curr]); 
        Update(1,N,1,curr,arr[curr]); 
    } 
  
    ll ans = 0; 
  
    for(ll i:dp) {
        // po(ans);
        ans = max(i, ans); 
    }
    pp(ans);

     //cout<<"Case #"<<tt<<": ";  
     }
 
    return 0;
}

Measured memory_bytes: 12636