We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
DFS memoization+BufferedReader,StringTokenizer: but iterative approach recommended
publicclassSolution{privatestaticbooleancheck(inti,intleap,int[]arr){if(i<0||arr[i]==1)returnfalse;if(i+leap>=arr.length||i==arr.length-1)returntrue;arr[i]=1;returncheck(i+leap,leap,arr)||check(i+1,leap,arr)||check(i-1,leap,arr);}publicstaticvoidmain(String[]args)throwsIOException{/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */BufferedReaderbr=newBufferedReader(newInputStreamReader(System.in));intt=Integer.parseInt(br.readLine().trim());while(t-->0){StringTokenizerst=newStringTokenizer(br.readLine());intn=Integer.parseInt(st.nextToken());intleap=Integer.parseInt(st.nextToken());st=newStringTokenizer(br.readLine());int[]arr=newint[n];for(inti=0;i<n;i++){arr[i]=Integer.parseInt(st.nextToken());}System.out.println(Solution.check(0,leap,arr)?"YES":"NO");}}}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Java 1D Array (Part 2)
You are viewing a single comment's thread. Return to all comments →
DFS memoization+BufferedReader,StringTokenizer: but iterative approach recommended