diff --git a/OneDArray.class b/OneDArray.class new file mode 100644 index 0000000..a87ee23 Binary files /dev/null and b/OneDArray.class differ diff --git a/OneDArray.java b/OneDArray.java new file mode 100644 index 0000000..d7c05cb --- /dev/null +++ b/OneDArray.java @@ -0,0 +1,40 @@ +import java.util.Scanner; + +public class OneDArray { + public static boolean canWin(int leap, int[] game, int index) { + // Base cases + if (index < 0 || game[index] == 1) { + return false; + } + if (index + 1 >= game.length || index + leap >= game.length) { + return true; + } + + // Mark current position as visited + game[index] = 1; + + // Recursive cases + return canWin(leap, game, index - 1) || // Move backward + canWin(leap, game, index + 1) || // Move forward + canWin(leap, game, index + leap); // Jump forward + } + + public static boolean canWin(int leap, int[] game) { + return canWin(leap, game, 0); // Start from index 0 + } + + 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(); + } +}