-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJava 1D Array (Part 2)
More file actions
52 lines (40 loc) · 1.86 KB
/
Copy pathJava 1D Array (Part 2)
File metadata and controls
52 lines (40 loc) · 1.86 KB
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
43
44
45
46
47
48
49
50
51
52
/*
Let's play a game on an array! You're standing at index of an -element array named . From some index (where ), you can perform one of the following moves:
Move Backward: If cell exists and contains a , you can walk back to cell .
Move Forward:
If cell contains a zero, you can walk to cell .
If cell contains a zero, you can jump to cell .
If you're standing in cell or the value of , you can walk or jump off the end of the array and win the game.
In other words, you can move from index to index , , or as long as the destination index is a cell containing a . If the destination index is greater than , you win the game.
Given and , complete the function in the editor below so that it returns true if you can win the game (or false if you cannot).
Input Format
The first line contains an integer, , denoting the number of queries (i.e., function calls).
The subsequent lines describe each query over two lines:
The first line contains two space-separated integers describing the respective values of and .
The second line contains space-separated binary integers (i.e., zeroes and ones) describing the respective values of .
Constraints
It is guaranteed that the value of is always .
Output Format
Return true if you can win the game; otherwise, return false.
Sample Input
4
5 3
0 0 0 0 0
6 5
0 0 0 1 1 1
6 3
0 0 1 1 1 0
3 1
0 1 0
Sample Output
YES
YES
NO
NO
Explanation
We perform the following queries:
For and , we can walk and/or jump to the end of the array because every cell contains a . Because we can win, we return true.
For and , we can walk to index and then jump units to the end of the array. Because we can win, we return true.
For and , there is no way for us to get past the three consecutive ones. Because we cannot win, we return false.
For and , there is no way for us to get past the one at index . Because we cannot win, we return false.
*/