Determine whether forward jumps can reach the final position from the start of an array.
Problem
Input. A nonempty list nums of nonnegative integers, where nums[i] is the maximum jump length from position i.
Output. Return True if some sequence of forward jumps reaches the final index; otherwise return False.
Rules. Start at index zero. A jump may have any positive length up to the current position’s value.
The listed lengths are maxima, not mandatory exact distances. A zero cannot advance the position.
Leave the input unchanged. A singleton returns True because its start is already the destination.
Example. [2, 3, 1, 1, 4] returns True, using jumps from index 0 to 1 to 4.
For [3, 2, 1, 0, 4], return False: no reachable position can pass index 3.
Picture
The first value 3 permits landings 1, 2 and 3, filling the reachable prefix.
Each later reachable position contributes another interval starting within that prefix.
Their union stays contiguous, so the largest endpoint summarizes existence without preserving every jump choice.
After processing indices 0,1,2,3, farthest is always 3.
Index 4 lies outside that boundary.
Its own value 4 cannot invent a path to itself.
Problem trace
A large value cannot help if its index is unreachable
Reference drawing
Problem trace
A large value cannot help if its index is unreachable
Execution trace
- index
- 0
- farthest
- 0
- example
- nums = [3, 2, 1, 0, 4]
- farthest
- 0→3
- check
- not shown→0 <= 0: reachable
- update
- not shown→max(0, 0 + 3) = 3
- index
- 0→1
- check
- 0 <= 0: reachable→1 <= 3: reachable
- update
- max(0, 0 + 3) = 3→max(3, 1 + 2) = 3
- index
- 1→2
- check
- 1 <= 3: reachable→2 <= 3: reachable
- update
- max(3, 1 + 2) = 3→max(3, 2 + 1) = 3
- index
- 2→3
- check
- 2 <= 3: reachable→3 <= 3: reachable
- update
- max(3, 2 + 1) = 3→max(3, 3 + 0) = 3
- index
- 3→4
- check
- 3 <= 3: reachable→4 > 3: unreachable
- branch
- not shown→return before update
- Recognize it
- Each array value is a maximum forward jump and the question asks only whether the final position is reachable, not for a path or minimum jump count.
- Keep true
- Before index i is processed, every position through farthest is reachable using processed jumps; if i exceeds farthest, no earlier choice can reach i or anything beyond it.
- Reuse it
- When all feasible positions form a prefix, summarize every prior choice by its farthest boundary; this transfers to interval coverage, refueling reach, and minimum-jump layer scans.
Before an expansion, check index <= farthest.
Only then may index+jump extend the boundary.
The scalar stores reachability, not an explicit path or minimum jump count.
Code
def can_jump(nums: list[int]) -> bool:
farthest = 0
for index, jump in enumerate(nums):
if index > farthest:
return False
farthest = max(farthest, index + jump)
return True
Python’s enumerate retrieves a value before the check; an unreachable value is rejected before use in expansion.
The function uses scalar work and scalar slots.
Empty input returns True by its literal convention; it has no destination index.
A single [0] also returns True.
Failure Check
With maximum jumps, [2,3,0,0,0] can use 0 -> 1 -> 4.
With exact jumps, it is trapped at index 2.
Exact distances, forbidden landings or backward moves require a new sufficiency argument.
Change the Destination
New task: Check a specified integer destination index.
Picture and change: Validate the target, then stop when the reachable boundary covers it. No earlier source can bridge a gap beyond that boundary.
def can_reach_target(nums, target):
if not 0 <= target < len(nums):
return False
farthest = 0
for index, jump in enumerate(nums):
if index > farthest:
return False
farthest = max(farthest, index + jump)
if farthest >= target:
return True
return False
Check: [3,2,1,0,4] reaches target 3 but not target 4.
An invalid target returns False, including every target for empty input.
Use when: Only the destination changes.
Change to Minimum Jumps
New task: Return minimum hop count, or None when no destination is reachable.
Picture and change: Keep the current layer’s end and the next layer’s farthest reach. Increment the count only after scanning a whole equal-cost layer. Stalled growth means no next layer exists.
def minimum_jumps(nums):
if not nums:
return None
jumps, layer_end, farthest = 0, 0, 0
for index in range(len(nums) - 1):
if index > farthest:
return None
farthest = max(farthest, index + nums[index])
if index == layer_end:
if farthest == layer_end:
return None
jumps += 1
layer_end = farthest
return jumps
Check: [2,3,1,1,4] returns 2; [3,2,1,0,4] returns None; [0] returns 0.
Both adaptations retain linear scalar work and constant scalar slots under the maximum-jump contract.
Layer boundaries add meaning absent from the Boolean reference.
Use when: Minimum equal-cost layers are required.
Transfer Check
Why does the farthest boundary not reconstruct a path?
Answer
It discards which predecessor first reached each destination. A witness needs predecessor information or another reconstruction method.