Given an array of non-negative integers arr, you are initially positioned at start index of the array. When you are at index i, you can jump to i + arr[i] or i - arr[i], check if you can reach any index with value 0.
Notice that you can not jump outside of the array at any time.
Example 1:
Input: arr = [4,2,3,0,3,1,2], start = 5 Output: true Explanation: All possible ways to reach at index 3 with value 0 are: index 5 -> index 4 -> index 1 -> index 3 index 5 -> index 6 -> index 4 -> index 1 -> index 3
Example 2:
Input: arr = [4,2,3,0,3,1,2], start = 0 Output: true Explanation: One possible way to reach at index 3 with value 0 is: index 0 -> index 4 -> index 1 -> index 3
Example 3:
Input: arr = [3,0,2,1,2], start = 2 Output: false Explanation: There is no way to reach at index 1 with value 0.
Constraints:
1 <= arr.length <= 5 * 1040 <= arr[i] < arr.length0 <= start < arr.lengthWhen you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
The basic idea is to explore every possible path we can take. We'll start at the beginning and see where we can jump, then from each of those places, see where we can jump next, and so on, until we find our target or run out of options.
Here's how the algorithm would work step-by-step:
def jump_game_iii_brute_force(array, start_index):
# Use a stack to keep track of the indices to visit.
indices_to_visit = [start_index]
while indices_to_visit:
current_index = indices_to_visit.pop()
if array[current_index] == 0:
return True
jump_distance = array[current_index]
# Prevents infinite loops by marking index as visited implicitly
array[current_index] = -array[current_index]
forward_index = current_index + jump_distance
backward_index = current_index - jump_distance
# Check if the forward jump is valid.
if 0 <= forward_index < len(array) and array[forward_index] >= 0:
indices_to_visit.append(forward_index)
# Check if the backward jump is valid.
if 0 <= backward_index < len(array) and array[backward_index] >= 0:
indices_to_visit.append(backward_index)
# No path found to an index with value 0.
return FalseThe key is to explore the possible jumps strategically, marking locations we've already visited to avoid repeating work. We start at the given location and systematically check where we can jump, until we find a location that holds the magic number.
Here's how the algorithm would work step-by-step:
def jump_game_iii(array, start_index):
array_length = len(array)
visited_indices = [False] * array_length
def can_reach_zero(current_index):
# Check if the index is out of bounds.
if current_index < 0 or current_index >= array_length:
return False
# Base case: we found the target value (0).
if array[current_index] == 0:
return True
# If we've visited this index, avoid infinite loops.
if visited_indices[current_index]:
return False
# Mark the current index as visited.
visited_indices[current_index] = True
jump_distance = array[current_index]
# Explore both possible jump directions.
return can_reach_zero(current_index + jump_distance) or \
can_reach_zero(current_index - jump_distance)
return can_reach_zero(start_index)| Case | How to Handle |
|---|---|
| Null or empty input array | Return false immediately as there are no indices to explore. |
| Single element array where the element is 0 and start index is 0 | Return true as the goal is already reached at the start. |
| Single element array where the element is not 0, regardless of start index | Return false as reaching 0 is impossible. |
| Start index is out of bounds | Return false if start index is invalid (less than 0 or greater than or equal to array length). |
| Array contains cycles (e.g., jumping back and forth between two indices) | Use a visited array/set to prevent infinite loops by marking visited indices. |
| No index with value 0 exists in the array, but a valid path exists without landing on 0. | The algorithm should still terminate and return false when all reachable nodes have been explored without finding a 0. |
| Large array size to test for stack overflow with recursive solutions. | Prefer an iterative solution (e.g., BFS) to avoid potential stack overflow errors. |
| Array with extreme jump lengths potentially leading to integer overflow when calculating new indices. | Ensure index calculations (i + arr[i], i - arr[i]) do not overflow, potentially using long data types or checking for overflow before performing jumps. |