We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
private static boolean canWinFromPosition(int leap, int[] game, int position) {
// Base cases
if (position >= game.length) {
return true; // You have won by reaching beyond the last index
}
if (position < 0 || game[position] == 1) {
return false; // You are either out of bounds or hit a forbidden spot
}
// Mark the current position as visited
game[position] = 1;
// Explore all possible moves
return canWinFromPosition(leap, game, position + leap) ||
canWinFromPosition(leap, game, position + 1) ||
canWinFromPosition(leap, game, position - 1);
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Java 1D Array (Part 2)
You are viewing a single comment's thread. Return to all comments →
//Right code
return canWinFromPosition(leap, game, 0); }