Java 1D Array (Part 2)

Sort by

recency

|

630 Discussions

|

  • + 0 comments

    import java.util.*;

    public class Solution {

    public static boolean canWin(int leap, int[] game) {
        // Return true if you can win the game; otherwise, return false.
    
       int n=game.length;
       boolean []visited = new boolean[game.length];
       Queue<Integer> quee = new LinkedList<Integer>();
    
       quee.add(0);
       while(quee.size() !=0){
            int i=quee.poll();
            if(!visited[i]){
            if(i+leap>=n || i + 1 >= n) {
                return true;
            }
    
            else {
                visited[i]=true;
                if(i+leap<n){
                    if(game[i+leap]==0){
                    quee.add(i+leap);
                }
                }
    
                if(game[i+1]==0){
                    quee.add(i+1);
                }
                if(i>0){if(game[i-1]==0){
                    quee.add(i-1);
                }}
    
    
            }
       }
       }
    
    
    
    
        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();
    }
    

    }

  • + 0 comments

    What is the issue with my iterative approach as 5 test cases are failing?

    public static boolean canWin(int leap, int[] game) {
            int n = game.length;
            boolean[] visited = new boolean[n];
            int i = 0; 
            while (i < n) {
                if (i+1 >= n || i + leap >= n) {
                    return true;
                }
                if (visited[i]) {
                    return false;
                }
                visited[i] = true;
    
                if (i + leap < n && game[i + leap] == 0 && !visited[i + leap]) {
                    i += leap;  
                }
                else if (i + 1 < n && game[i + 1] == 0 && !visited[i + 1]) {
                    i++;  
                }
                else if (i - 1 >= 0 && game[i - 1] == 0 && !visited[i - 1]) {
                    i--; 
                }
                else {
                    return false;
                }
    	return false;
    }
    
  • + 0 comments
    public static boolean canWin(int leap, int[] game) {
        // Return true if you can win the game; otherwise, return false.
        return canWinHelper(0, leap, game);
    }
    
    static boolean canWinHelper(int currentIndex, int leap, int[] game) {
        if( currentIndex >= game.length || game[currentIndex]==1 ){
            return false;
        }
        if (currentIndex == game.length - 1 || currentIndex + leap >= game.length) {
            return true;
        }
        game[currentIndex] = 1; 
        boolean canWinFromNext = canWinHelper(currentIndex + 1, leap, game);
        boolean canWinFromBack = (currentIndex - 1 >= 0) && canWinHelper(currentIndex - 1, leap, game);
        boolean canWinFromLeap = canWinHelper(currentIndex + leap, leap, game);
        game[currentIndex] = 0; 
        return canWinFromNext || canWinFromBack || canWinFromLeap;
    }
    
  • + 0 comments

    public static boolean canWin(int leap, int[] game) { return canWinHelper(leap, game, 0); }

    // Helper method to recursively check if the game can be won private static boolean canWinHelper(int leap, int[] game, int pos) { // Base cases if (pos < 0 || game[pos] == 1) return false; // Out of bounds or already visited if (pos >= game.length - 1 || pos + leap >= game.length) return true; // Win condition

    // Mark the current position as visited
    game[pos] = 1;
    
    // Recursive checks for possible moves
    return canWinHelper(leap, game, pos + leap) || // Jump forward
           canWinHelper(leap, game, pos + 1) ||   // Walk forward
           canWinHelper(leap, game, pos - 1);     // Walk backward
    

    }

  • + 0 comments

    import java.util.*;

    public class Solution {

    public static boolean canWin(int leap, int[] game) {
        // Return true if you can win the game; otherwise, 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();
    }
    

    }