Problem:
Score : 400 points

Problem Statement

You are given a sequence A_1, A_2, ..., A_N and an integer K.

Print the maximum possible length of a sequence B that satisfies the following conditions:

B is a (not necessarily continuous) subsequence of A.

For each pair of adjacents elements of B, the absolute difference of the elements is at most K.

Constraints

1 \leq N \leq 300,000

0 \leq A_i \leq 300,000

0 \leq K \leq 300,000

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N K
A_1
A_2
:
A_N

Output

Print the answer.

Sample Input 1

10 3
1
5
4
3
8
6
9
7
2
4

Sample Output 1

7

For example, B = (1, 4, 3, 6, 9, 7, 4) satisfies the conditions.

It is a subsequence of A = (1, 5, 4, 3, 8, 6, 9, 7, 2, 4).

All of the absolute differences between two adjacent elements (|1-4|, |4-3|, |3-6|, |6-9|, |9-7|, |7-4|) are at most K = 3.

Solution in Java (OpenJDK 1.8.0) (judge verdict: Wrong Answer)

import java.util.*;
import java.io.*;
import java.math.*;

public class Main 

{ 
static int mod=1000000007;
	 static FastReader sc=new FastReader(); 
	 
		  public static void main(String[] args) 
    {
			  //PrintWriter out=new PrintWriter(System.out);
			 // StringBuffer sb=new StringBuffer("");
			    int t = 1; 
		        while (t-- > 0) 
				{ 
		        	
		        		
		        	int n=i();
		        	int k=i();
		        	int A[]=input(n);
		        	int dp[]=new int[n+1];
		        	Arrays.fill(dp, -1);
		        	System.out.println(c(A,0,n,k,dp));
		        			
		        	
		        	
		        	
		        	
		        	
		       
		        	
		        	
				}
		        
		        
		        
		       // System.out.println(sb.toString());
		        //out.close();
			  
			  
			  
    }
		  
		  static int c(int A[],int i,int n,int k,int dp[]){
			  if(dp[i]!=-1) {
				  return dp[i];
			  }
			    if(i>=n)
			    return 0;
			    int y=A[i];
			    int ind=-1;
			    for(int j=i+1;j<n;j++){
			        if(Math.abs(y-A[j])<=k){
			            ind=j;
			            break;
			        }
			    }
			    int op1;
			    if(ind==-1&&i==n-1)
			    	op1=1;
			    else if(ind==-1)
			    	op1=0;
			    else
			     op1=1+c(A,ind,n,k,dp);
			    int op2=c(A,i+1,n,k,dp);
			    dp[i]= Math.max(op1,op2);
			    return dp[i];
			    
			}
		  
		  
		  
		  
		  static boolean Prime(int n) 
		    { 
		        if (n <= 1) 
		            return false; 
		        if (n <= 3) 
		            return true; 
		        if (n % 2 == 0 || n % 3 == 0) 
		            return false; 
		  
		        for (int i = 5; i * i <= n; i = i + 6) 
		            if (n % i == 0 || n % (i + 2) == 0) 
		                return false; 
		  
		        return true; 
		    } 
static int[] input(int n) {
	int A[]=new int[n];
	   for(int i=0;i<n;i++) {
		   A[i]=sc.nextInt();
	   }
	   return A;
   }
static long[] inputL(int n) {
	long A[]=new long[n];
	   for(int i=0;i<n;i++) {
		   A[i]=sc.nextLong();
	   }
	   return A;
   }
static String[] inputS(int n) {
	String A[]=new String[n];
	   for(int i=0;i<n;i++) {
		   A[i]=sc.next();
	   }
	   return A;
   }


static void input(int A[],int B[]) {
	   for(int i=0;i<A.length;i++) {
		   A[i]=sc.nextInt();
		   B[i]=sc.nextInt();
	   }
}
static long mod(long x) {
	  return ((x%mod + mod)%mod);
}

     static int i() {
    	 return sc.nextInt();
     }
     static String s() {
    	 return sc.next();
     }
     static long l() {
    	 return sc.nextLong();
     }  
     static void sort(int[] arr){
        int n = arr.length;
        Random rnd = new Random();
        for(int i=0; i<n; ++i){
            int tmp = arr[i];
            int randomPos = i + rnd.nextInt(n-i);
            arr[i] = arr[randomPos];
            arr[randomPos] = tmp;
        }
        Arrays.sort(arr);
     }
     static int gcd(int a, int b) 
     { 
         if (a == 0) 
             return b; 
         return gcd(b % a, a); 
     } 
  
        
        
    static class FastReader 
    { 
        BufferedReader br; 
        StringTokenizer st; 
  
        public FastReader() 
        { 
            br = new BufferedReader(new
                     InputStreamReader(System.in)); 
        } 
  
        String next() 
        { 
            while (st == null || !st.hasMoreElements()) 
            { 
                try
                { 
                    st = new StringTokenizer(br.readLine()); 
                } 
                catch (IOException  e) 
                { 
                    e.printStackTrace(); 
                } 
            } 
            return st.nextToken(); 
        } 
  
        int nextInt() 
        { 
            return Integer.parseInt(next()); 
        } 
  
        long nextLong() 
        { 
            return Long.parseLong(next()); 
        } 
  
        double nextDouble() 
        { 
            return Double.parseDouble(next()); 
        } 
  
        String nextLine() 
        { 
            String str = ""; 
            try
            { 
                str = br.readLine(); 
            } 
            catch (IOException e) 
            { 
                e.printStackTrace(); 
            } 
            return str; 
        } 
    } 
}