Jump Game III (LeetCode 1306): Solution and Explanation
From index i you may jump to i + arr[i] or i - arr[i], never leaving the array. Starting at start, decide whether any index holding 0 is reachable.
Intuition
Each index has at most two moves out of it, and those moves never change. That is a directed graph with n nodes and up to 2n edges, hidden inside an array.
Reaching a zero is just reachability in that graph. A BFS or DFS from start that marks indices as it visits them will find a zero if one is reachable.
Marking matters: without it, a pair of indices that jump to each other would loop forever. With it, every index is processed at most once, so the search is linear.
Approach: Breadth-first search over indices
- Put start in a queue and mark it seen.
- Dequeue an index i. If arr[i] is 0, return true.
- For each of i + arr[i] and i - arr[i], if it is inside the array and not seen, mark it and enqueue it.
- If the queue empties without finding a zero, return false.
Step-by-step example
arr = [4,2,3,0,3,1,2], start = 5
- Dequeue 5 (value 1). Jumps go to 6 and 4; both are new, enqueue them.
- Dequeue 6 (value 2). 8 is off the array; 4 is already seen.
- Dequeue 4 (value 3). 7 is off the array; 1 is new, enqueue it.
- Dequeue 1 (value 2). 3 is new, enqueue it; -1 is off the array.
- Dequeue 3. arr[3] is 0, so return true. The route was 5 -> 4 -> 1 -> 3.
Code: Python, Java, C++
1class Solution:
2 def canReach(self, arr: List[int], start: int) -> bool:
3 n = len(arr)
4 seen = [False] * n
5 queue = deque([start])
6 seen[start] = True
7 while queue:
8 i = queue.popleft()
9 if arr[i] == 0:
10 return True
11 for j in (i + arr[i], i - arr[i]):
12 if 0 <= j < n and not seen[j]:
13 seen[j] = True
14 queue.append(j)
15 return False1class Solution {
2 public boolean canReach(int[] arr, int start) {
3 int n = arr.length;
4 boolean[] seen = new boolean[n];
5 Deque<Integer> queue = new ArrayDeque<>();
6 queue.offer(start);
7 seen[start] = true;
8 while (!queue.isEmpty()) {
9 int i = queue.poll();
10 if (arr[i] == 0) {
11 return true;
12 }
13 int[] nexts = {i + arr[i], i - arr[i]};
14 for (int j : nexts) {
15 if (j >= 0 && j < n && !seen[j]) {
16 seen[j] = true;
17 queue.offer(j);
18 }
19 }
20 }
21 return false;
22 }
23}1class Solution {
2public:
3 bool canReach(vector<int>& arr, int start) {
4 int n = arr.size();
5 vector<bool> seen(n, false);
6 queue<int> q;
7 q.push(start);
8 seen[start] = true;
9 while (!q.empty()) {
10 int i = q.front();
11 q.pop();
12 if (arr[i] == 0) {
13 return true;
14 }
15 int nexts[2] = {i + arr[i], i - arr[i]};
16 for (int j : nexts) {
17 if (j >= 0 && j < n && !seen[j]) {
18 seen[j] = true;
19 q.push(j);
20 }
21 }
22 }
23 return false;
24 }
25};Time and space complexity
O(n)
Each index is enqueued at most once and tries two jumps.
O(n)
The seen array and the queue each hold at most n indices.
Other approaches
Recursive DFS (time O(n), space O(n))
Recurse into both jumps, marking visited indices (for example by negating arr[i]). Shorter code, but on n = 50,000 a long chain can exceed the recursion limit.
Iterative DFS with a stack (time O(n), space O(n))
Identical to the BFS with the queue swapped for a stack. It reaches the same set of indices, just in a different order.
Common mistakes
- Forgetting to mark visited indices, which loops forever on cycles like [1, 1] or any i where a jump returns to an earlier index.
- Treating a jump outside the array as allowed. Both i + arr[i] >= n and i - arr[i] < 0 must be skipped.
- Checking only arr[start] or only the forward direction. Jumps can go left as well as right.
- Marking an index only when it is dequeued, which lets the same index be enqueued many times.
Frequently asked questions
Why is this a graph problem when the input is an array?
Each index is a node and each allowed jump is an edge. The array just encodes the adjacency list compactly, two edges per node.
Does BFS find the fewest jumps here?
Yes, BFS reaches every index by the fewest jumps, but the problem only asks whether a zero is reachable, so DFS works just as well.
Can I mark visited without extra memory?
Yes. Because values are non-negative, you can negate arr[i] after visiting it and treat negative values as seen. That changes the input, so only do it if that is allowed.