Java 1D Array (Part 2)

  • + 1 comment

    My code has passed 5 test cases . it is failing the test cases which has 5000 test cases . does this has to do any thing with that ??i even tried running 5000 test cases but i am getting output for 1473 records can any body try my code and tell me atleast what is happening and why just 1473 test cases are executing ????? +rshaghoulian(appriciate if you can help me)

    import java.util.*;
    
    public class Solution {
        public static boolean canWin(int leap, int[] game) {
        	
        	
    		for(int i=0, j=0;j<game.length &&i<game.length;j++){
    			if(i+leap>=game.length){
    				return true;
    			}else if(game[i+leap]==0){
    				i=i+leap;
    				if(i<game.length-1 &&i+leap<game.length ){
    					while(game[i]==0 &&i>0 ){
    						i--;
    					}
    					i++;
    
    				}
    
    			}else if(i+1<game.length&&game[i+1]==0){
    				i=i+1;
    
    			}else if(i+1<game.length&&game[i+1]==1){
    				return false;
    			}else{
    				return false;
    			}
    
    
    
    		}return false;
        }
        public static void main(String[] args) {
            Scanner scan = new Scanner(System.in);
            int q = scan.nextInt();
            while (q-- > 0) {
                int n = scan.nextInt();
                int leap = scan.nextInt();
    
                int[] game = new int[n];
                for (int i = 0; i < n; i++) {
                    game[i] = scan.nextInt();
                }
    
                System.out.println( (canWin(leap, game)) ? "YES" : "NO" );
            }
            scan.close();
        }
    }