Please do not copy from other sources. I provided 2 pictures of the question and part of the code. Please complete it by your own. Here is a part of the code for your privilege : import java.util.*;...



Please do not copy from other sources.


I provided 2 pictures of the question and part of the code. Please complete it by your own.


Here is a part of the code for your privilege :



import java.util.*;


public class Solution {


public static boolean canWin(int leap, int[] game) {
// Return true if you can win the game; otherwise, return false.
}


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();
}
}


Let's play a game on an array! You're standing at index 0 of an n-element array named game. From some<br>index i (where 0 <i< n), you can perform one of the following moves:<br>• Move Backward: If cell i – 1 exists and contains a 0, you can walk back to cell i – 1.<br>• Move Forward:<br>• If cell i +1 contains a zero, you can walk to cell i +1.<br>If cell i + leap contains a zero, you can jump to cell i + leap.<br>• If you're standing in cell n – 1 or the value of i + leap > n, you can walk or jump off the end of the<br>array and win the game.<br>In other words, you can move from index i to index i + 1. i – 1, or i + leap as long as the destination index<br>is a cell containing a 0. If the destination index is greater than n – 1. you win the game.<br>Function Description<br>Complete the canWin function in the editor below.<br>canWin has the following parameters:<br>• int leap: the size of the leap<br>• int game[n]: the array to traverse<br>Returns<br>• boolean: true if the game can be won, otherwise false<br>Input Format<br>The first line contains an integer, q. denoting the number of queries (i.e., function calls).<br>The 2 · q subsequent lines describe each query over two lines:<br>1. The first line contains two space-separated integers describing the respective values of n and leap.<br>2. The second line contains n space-separated binary integers (i.e., zeroes and ones) describing the respective<br>values of gameo, game1, ..., gamen-1.<br>

Extracted text: Let's play a game on an array! You're standing at index 0 of an n-element array named game. From some index i (where 0 n, you can walk or jump off the end of the array and win the game. In other words, you can move from index i to index i + 1. i – 1, or i + leap as long as the destination index is a cell containing a 0. If the destination index is greater than n – 1. you win the game. Function Description Complete the canWin function in the editor below. canWin has the following parameters: • int leap: the size of the leap • int game[n]: the array to traverse Returns • boolean: true if the game can be won, otherwise false Input Format The first line contains an integer, q. denoting the number of queries (i.e., function calls). The 2 · q subsequent lines describe each query over two lines: 1. The first line contains two space-separated integers describing the respective values of n and leap. 2. The second line contains n space-separated binary integers (i.e., zeroes and ones) describing the respective values of gameo, game1, ..., gamen-1.
Constraints<br>•1<q< 5000<br>• 2 <n< 100<br>•0 < leap < 100<br>• It is guaranteed that the value of game[0] is always 0.<br>Sample Input<br>STDIN<br>Function<br>---- ----<br>q = 4 (number of queries)<br>game [] size n = 5, leap = 3 (first query)<br>game = [0, 0, 0, 0, 0]<br>game [] size n = 6, leap = 5 (second query)<br>4<br>5 3<br>θ 0 0 0 0<br>6 5<br>0 0 0 11 1<br>6 3<br>® 0 111 0<br>3 1<br>O 10<br>Sample Output<br>YES<br>YES<br>NO<br>NO<br>Explanation<br>We perform the following q = 4 queries:<br>1. For game<br>[0,0,0,0,0] and leap = 3, we can walk and/or jump to the end of the array because every<br>cell contains a 0. Because we can win, we return true.<br>2. For game = [0,0,0, 1, 1, 1] and leap = 5, we can walk to index 1 and then jump i + leap = 1+ 5 = 6<br>units to the end of the array. Because we can win, we return true.<br>3. For game = [0, 0, 1, 1, 1, 0] and leap = 3, there is no way for us to get past the three consecutive ones.<br>Because we cannot win, we return false.<br>4. For game = [0, 1, 0] and leap = 1, there is no way for us to get past the one at index 1. Because we<br>cannot win, we return false.<br>

Extracted text: Constraints •1
Jun 10, 2022
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here