Jump Game III (LeetCode 1306): Solution and Explanation

Medium
Array
Depth First Search
Breadth First Search

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

  1. Put start in a queue and mark it seen.
  2. Dequeue an index i. If arr[i] is 0, return true.
  3. For each of i + arr[i] and i - arr[i], if it is inside the array and not seen, mark it and enqueue it.
  4. If the queue empties without finding a zero, return false.

Step-by-step example

arr = [4,2,3,0,3,1,2], start = 5
  1. Dequeue 5 (value 1). Jumps go to 6 and 4; both are new, enqueue them.
  2. Dequeue 6 (value 2). 8 is off the array; 4 is already seen.
  3. Dequeue 4 (value 3). 7 is off the array; 1 is new, enqueue it.
  4. Dequeue 1 (value 2). 3 is new, enqueue it; -1 is off the array.
  5. 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 False
Press play on the visualizer and the running line lights up here.

Time and space complexity

Time

O(n)

Each index is enqueued at most once and tries two jumps.

Space

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.