Jump Game visualization

Can you reach the last index if A[i] is the max jump length from i? Track the farthest reach.

Scan left to right keeping farthest = max(i + A[i]) over every index we can reach. If i ever passes farthest, we are stuck.

Edge cases: a single element is already at the end; a 0 that every path must land on blocks you; negative jump lengths are treated as 0 (you cannot jump backwards here).

Complexity

Time: best O(1), average O(n), worst O(n). Space: O(1). Difficulty: Intermediate.

Pseudocode

procedure canJump(A):
  farthest ← 0
  for i ← 0 to n−1:
    if i > farthest: return false        // unreachable
    farthest ← max(farthest, i + A[i])
    if farthest ≥ n−1: return true
  return true

More greedy algorithms