Skip to content

Jump Game

Given an integer array nums where you are initially positioned at the array’s first index, each element in the array represents your maximum jump length from that position.

Return true if you can reach the last index, or false otherwise.

InputOutputExplanation
[2, 3, 1, 1, 4]trueJump 1 step from index 0 to 1, then 3 steps to the last index.
[3, 2, 1, 0, 4]falseYou always arrive at index 3; its max jump length is 0, so you cannot reach the last index.
[0]trueAlready at the last index.
[2, 0, 0]trueJump 1 step from index 0 to 1, then no more jumps needed.
[0, 2, 3]falseStuck at index 0; can only jump 0 steps.
  • 1 <= nums.length <= 10^4
  • 0 <= nums[i] <= 10^5
ApproachTimeSpaceBest When
Greedy (Backwards)O(n)O(1)You want the most intuitive backwards-thinking approach
DP (Forward)O(n)O(1)You prefer forward iteration or early termination optimization

Both approaches are equally efficient and optimal.

Most Intuitive
Greedy Backwards
O(n) time · O(1) space
Forward Scanning
DP Forward
O(n) time · O(1) space
★ Recommended

Start at the last index and work backwards. Track the leftmost “good” index that can reach the end. If you can reach index 0, you can reach the last index from the start.

The key insight: if you’re at position i and your max jump is nums[i], you can reach any position from i to i + nums[i]. Working backwards, you ask: “Can I reach my current good index from this position?”

⏱ Time O(n) Single backwards pass 💾 Space O(1) Only counter variable

Input: nums = [2, 3, 1, 1, 4]

Stepinums[i]Reach (i + nums[i])Can reach lastGood?lastGoodIndex
14484
2314✓ (4 >= 4)3
3213✓ (3 >= 3)2
4134✓ (4 >= 2)1
5022✓ (2 >= 1)0

Result: lastGoodIndex == 0, so return true

Input: nums = [3, 2, 1, 0, 4]

Stepinums[i]Reach (i + nums[i])Can reach lastGood?lastGoodIndex
14484
2303✗ (3 < 4)4
3213✗ (3 < 4)4
4123✗ (3 < 4)4
5033✗ (3 < 4)4

Result: lastGoodIndex == 4 != 0, so return false

Animated walkthrough

How the backwards greedy approach finds reachability

Watch the lastGoodIndex shrink as we move backwards through the array. When we reach index 0, we know the answer.

Step 1 of 5 Target: Backwards Greedy
Waiting to begin...
Array state
Memory / lookup state

function canJumpGreedy(nums):
lastGoodIndex = len(nums) - 1
for i from len(nums) - 2 down to 0:
if i + nums[i] >= lastGoodIndex:
lastGoodIndex = i
return lastGoodIndex == 0
jump_game_greedy.py
from typing import List
def can_jump_greedy(nums: List[int]) -> bool:
"""
Greedy approach: work backwards from the end to find the furthest index
we can reach. If we can reach index 0, we can reach the end.
Time: O(n), Space: O(1)
"""
last_good_index = len(nums) - 1
for i in range(len(nums) - 2, -1, -1):
if i + nums[i] >= last_good_index:
last_good_index = i
return last_good_index == 0
# Test cases
print(can_jump_greedy([2, 3, 1, 1, 4])) # True
print(can_jump_greedy([3, 2, 1, 0, 4])) # False
print(can_jump_greedy([0])) # True
print(can_jump_greedy([2, 0, 0])) # True
print(can_jump_greedy([0, 2, 3])) # False
🎯 Interview Favourite

Scan the array left to right, tracking the furthest index you can reach so far. If you ever encounter an index beyond your reach, you’re stuck. If you reach or pass the last index, you win.

This approach can terminate early when maxReach >= len(nums) - 1, making it slightly more efficient in practice for arrays where the end is reachable early.

⏱ Time O(n) Single forward pass 💾 Space O(1) Only counter variable

Input: nums = [2, 3, 1, 1, 4]

Stepinums[i]maxReach (before)New maxReachi > maxReach?Continue?
10202
21324✓ (maxReach 4 >= 4) → return true

Input: nums = [3, 2, 1, 0, 4]

Stepinums[i]maxReach (before)New maxReachi > maxReach?Continue?
10303
21233
32133
43033
5443✓ → return false

At index 4, we exceed maxReach (3), so we are stuck.

function canJumpDP(nums):
maxReach = 0
for i from 0 to len(nums) - 1:
if i > maxReach:
return false
maxReach = max(maxReach, i + nums[i])
if maxReach >= len(nums) - 1:
return true
return false
jump_game_dp.py
from typing import List
def can_jump_dp(nums: List[int]) -> bool:
"""
Dynamic programming approach: forward pass tracking the furthest
reachable index. If we can ever reach the end index, return True.
Time: O(n), Space: O(1)
"""
max_reach = 0
for i in range(len(nums)):
if i > max_reach:
return False
max_reach = max(max_reach, i + nums[i])
if max_reach >= len(nums) - 1:
return True
return False
# Test cases
print(can_jump_dp([2, 3, 1, 1, 4])) # True
print(can_jump_dp([3, 2, 1, 0, 4])) # False
print(can_jump_dp([0])) # True
print(can_jump_dp([2, 0, 0])) # True
print(can_jump_dp([0, 2, 3])) # False
✓ Simple

A third approach demonstrating an alternative technique for solving this problem. This implementation optimizes either time or space complexity compared to the previous approaches.

⏱ Time O(n) Optimized complexity 💾 Space O(1) Efficient space usage
function approach3():
// Implementation approach goes here
jump_game_space_optimized.py
pass