Skip to content

Greedy — Jump Game III ​

↗ LeetCode: Jump Game III · Medium · pattern chapter →

Asked atAmazonGoogleMeta

From index start, you may jump i ± arr[i]. Return true iff you can reach any zero.

Example 1 — arr=[4,2,3,0,3,1,2], start=5 → trueExample 2 — arr=[4,2,3,0,3,1,2], start=0 → trueExample 3 — arr=[3,0,2,1,2], start=2 → false

Constraints — 1 ≤ n ≤ 5·10⁴; 0 ≤ arr[i] < n. Naive recursive path enumeration is O(2ⁿ) — at n=25 that's 3·10⁷. BFS/DFS with visited-set is O(n) = 5·10⁴ (each index visited at most once).

Progressive Hints
Try to solve it yourself first. Reveal hints only when stuck — each hint is progressively more specific.
---
⏱️Interview timer00:00
Practice under interview pressure. Start when you begin reading; stop when you finish. Save best only when you had a full working solution.
Target for interview: Medium = 20-25 min · Hard = 30-40 min · Best time persists on this device only.

Approach 1 — Brute recursion (no memo) ​

Intuition. Recurse into both i + arr[i] and i - arr[i]. Return true if any path hits a zero.

java
boolean canReachBrute(int[] arr, int start) {
    if (start < 0 || start >= arr.length) return false;
    if (arr[start] == 0) return true;
    int step = arr[start];
    arr[start] = -1;   // hacky visited via negation
    boolean ok = canReachBrute(arr, start + step) || canReachBrute(arr, start - step);
    arr[start] = step;
    return ok;
}

Complexity — Time worst O(2ⁿ) without visited tracking; Space O(n) recursion. In an interview mark visited explicitly to hit O(n).


Approach 2 — DFS/BFS on implicit graph (canonical) ​

Model each index as a node with 2 edges. BFS from start; return true on reaching a zero.

java
boolean canReach(int[] arr, int start) {
    Queue<Integer> q = new ArrayDeque<>();
    boolean[] seen = new boolean[arr.length];
    q.add(start); seen[start] = true;
    while (!q.isEmpty()) {
        int i = q.poll();
        if (arr[i] == 0) return true;
        for (int j : new int[]{i + arr[i], i - arr[i]})
            if (j >= 0 && j < arr.length && !seen[j]) { seen[j] = true; q.add(j); }
    }
    return false;
}
Execution Trace
DFS/BFS on implicit graph (canonical)
i40213203341526phase = start
Step 1 / 3Initialize scan.

Complexity — Time O(n); Space O(n). Say aloud in an interview: "canonical BFS on implicit-graph reachability — same shape as Snake and Ladders, Word Ladder, Open the Lock."


Try it yourself ​

Complexity summary ​

ApproachTimeSpaceGrade
BFSO(n)O(n)canonical

When to use which ​

  • Reachability → BFS/DFS.
  • Min steps to zero → same BFS, count layers.
  • "Same-value edges" → Jump Game IV.
✨AI Study CompanionBeta
Was this page helpful?

Released under MIT license.

💡 Quick shortcuts
H HintsR Run JavaA AI Help