- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHackerrankJava0031.java
More file actions
Latest commit
39 lines (31 loc) · 965 Bytes
/
Copy pathHackerrankJava0031.java
File metadata and controls
39 lines (31 loc) · 965 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
importjava.util.Scanner;
publicclassHackerrankJava0031 {
publicstaticvoidmain(String[] args) {
// Java 1D Array (Part 2)
// https://www.hackerrank.com/challenges/java-1d-array/problem?isFullScreen=true
Scannerscan = newScanner(System.in);
intq = scan.nextInt();
while (q-- > 0) {
intn = scan.nextInt();
intleap = scan.nextInt();
int[] game = newint[n];
for (inti = 0; i < n; i++) {
game[i] = scan.nextInt();
}
System.out.println((canWin(leap, game, 0)) ? "YES" : "NO");
}
scan.close();
}
publicstaticbooleancanWin(intleap, int[] game, intcurrentIndex) {
if (currentIndex < 0 || game[currentIndex] == 1) {
returnfalse;
}
if ((currentIndex == game.length - 1) || currentIndex + leap > game.length - 1) {
returntrue;
}
game[currentIndex] = 1;
returncanWin(leap, game, currentIndex + 1) ||
canWin(leap, game, currentIndex - 1) ||
canWin(leap, game, currentIndex + leap);
}
}