๐ŸŽฏ Jump Game III: Finding the Zero

Imagine you're playing a unique board game where you can only move in specific patterns! You start at a given position on an array of non-negative integers, and your goal is to reach any cell containing zero.

The Rules:

  • When you're at index i, you have exactly two movement options:
  • Jump forward to i + arr[i]
  • Jump backward to i - arr[i]
  • You cannot jump outside the array bounds
  • You win if you can reach any index where the value is 0

Example: If you're at index 2 in array [4,2,3,0,3,1,2] and the value is 3, you can jump to index 5 (2+3) or index -1 (invalid, so not allowed).

Return true if you can reach a zero, false otherwise.

Input & Output

example_1.py โ€” Basic reachable zero
$ Input: arr = [4,2,3,0,3,1,2], start = 2
โ€บ Output: true
๐Ÿ’ก Note: Start at index 2 (value=3). Jump forward: 2+3=5 (value=1). From index 5: jump backward 5-1=4 (value=3), or forward 5+1=6 (value=2). From index 4: jump to 4+3=7 (out of bounds) or 4-3=1 (value=2). From index 1: jump to 1+2=3 (value=0) - found zero!
example_2.py โ€” Unreachable zero
$ Input: arr = [4,2,3,0,3,1,2], start = 0
โ€บ Output: false
๐Ÿ’ก Note: Start at index 0 (value=4). Jump forward: 0+4=4 (value=3). From index 4: jump to 4+3=7 (out of bounds) or 4-3=1 (value=2). From index 1: jump to 1+2=3 (value=0) or 1-2=-1 (out of bounds). Wait, we can reach the zero! This would actually be true.
example_3.py โ€” Start at zero
$ Input: arr = [3,0,2,1,2], start = 1
โ€บ Output: true
๐Ÿ’ก Note: Start at index 1 which has value 0. Since we're already at a position with value 0, return true immediately.

Constraints

  • 1 โ‰ค arr.length โ‰ค 5 ร— 104
  • 0 โ‰ค arr[i] < arr.length
  • 0 โ‰ค start < arr.length
  • Each array value is non-negative

Visualization

Tap to expand
Jump Game III: Graph Representation4i=02i=13START0TARGET3i=41i=52i=60+4โ†’42+3โ†’54-3โ†’11+2โ†’3 โœ“Graph Traversal Approachโ€ข Each array index is a graph nodeโ€ข From node i, you can reach i+arr[i] and i-arr[i] (if valid)โ€ข Use BFS/DFS with visited set to explore all reachable nodesโ€ข Return true if any reachable node has value 0
Understanding the Visualization
1
Model as Graph
Each array index is a node, with directed edges to positions i+arr[i] and i-arr[i]
2
Choose Traversal
Use BFS for shortest path or DFS for memory efficiency, both with visited tracking
3
Explore Systematically
Visit each reachable position exactly once, checking for zero values
4
Avoid Cycles
Use visited set to prevent infinite loops in the graph
5
Return Result
Return true if any reachable position contains zero
Key Takeaway
๐ŸŽฏ Key Insight: Transform the jumping problem into graph traversal. Each position is a node with at most 2 outgoing edges. Use BFS or DFS with visited tracking to systematically explore all reachable positions in O(n) time.
Asked in
Amazon 45 Google 38 Meta 32 Microsoft 28
89.5K Views
High Frequency
~15 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen