-
Notifications
You must be signed in to change notification settings - Fork 3
/
Java1DArrayPartII.java
42 lines (33 loc) Β· 1.12 KB
/
Java1DArrayPartII.java
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
40
41
42
// https://www.hackerrank.com/challenges/java-1d-array/problem
import java.util.Scanner;
public class Java1DArrayPartII {
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();
}
private static boolean canWin(int leap, int[] game) {
return canWin(leap, game, 0);
}
private static boolean canWin(int leap, int[] array, int index) {
if (index < 0 || array[index] == 1) {
return false;
}
if (index == array.length - 1 || index + leap > array.length - 1) {
return true;
}
array[index] = 1;
return canWin(leap, array, index + 1)
|| canWin(leap, array, index - 1)
|| canWin(leap, array, index + leap);
}
}