Java 1D Array (Part 2)

Sort by

recency

|

635 Discussions

|

  • + 0 comments

    DFS memoization+BufferedReader,StringTokenizer: but iterative approach recommended

    public class Solution {
        private static boolean check(int i,int leap,int[] arr){
            if(i<0 || arr[i]==1) return false;
            if(i+leap>=arr.length || i==arr.length-1) return true;
            arr[i]=1;
            
            return check(i+leap,leap,arr)||
            check(i+1,leap,arr)||
            check(i-1,leap,arr);
        }
    
        public static void main(String[] args) throws IOException{
            /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            int t=Integer.parseInt(br.readLine().trim());
            while(t-->0){
                StringTokenizer st=new StringTokenizer(br.readLine());
                int n=Integer.parseInt(st.nextToken());
                int leap=Integer.parseInt(st.nextToken());
                st=new StringTokenizer(br.readLine());
                int[] arr=new int[n];
                for(int i=0;i<n;i++){
                    arr[i]=Integer.parseInt(st.nextToken());
                }
                System.out.println(Solution.check(0,leap,arr)?"YES":"NO");
                 
                
            }
            
        }
    }
    
  • + 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

  • + 1 comment

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

  • + 2 comments

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