Java 1D Array (Part 2)

Sort by

recency

|

634 Discussions

|

  • + 0 comments
    import java.util.*;
    
    public class Solution {
    
        private static boolean canWin(int leap, int[] game, boolean[] visited, int pos) {
            if (pos >= game.length) return true;
            if (pos < 0 || game[pos] != 0 || visited[pos]) return false;
            
            visited[pos] = true;
            
            return canWin(leap, game, visited, pos-1) || 
                canWin(leap, game, visited, pos+1) ||
                canWin(leap, game, visited, pos+leap);  
        }
    
        public static boolean canWin(int leap, int[] game) {
            return canWin(leap, game, new boolean[game.length], 0);
        }
    
        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

    Here is java 1D Array (Part 2) solution - https://programmingoneonone.com/hackerrank-java-1d-array-part-2-problem-solution.html

  • + 0 comments

    **Solved using JAVA. Easiest logic -https://javafunx.blogspot.com/2025/03/java-1d-array-solved.html

  • + 1 comment

    I don't understand this 1d array problem. anyone can explain this problem in simpleway.

  • + 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();
    }
    

    }