Jump Game in Python

The Jump Game is a classic dynamic programming problem where we need to determine if we can reach the last index of an array. Each element represents the maximum jump length from that position.

Given an array like [2,3,1,1,4], we start at index 0 and can jump at most 2 positions. From index 1, we can jump at most 3 positions, and so on. The goal is to reach the last index.

Algorithm Steps

The greedy approach works backwards from the last index ?

  • n := length of array A − 1
  • for i := n − 1, down to 0
    • if A[i] + i >= n, then n := i
  • return true when n = 0, otherwise false

Implementation

class Solution:
    def canJump(self, nums):
        n = len(nums) - 1
        for i in range(n - 1, -1, -1):
            if nums[i] + i >= n:
                n = i
        return n == 0

# Test the solution
solution = Solution()
result = solution.canJump([2, 3, 1, 1, 4])
print(f"Can reach last index: {result}")
Can reach last index: True

How It Works

Let's trace through the example [2,3,1,1,4] step by step ?

def canJump_with_trace(nums):
    n = len(nums) - 1
    print(f"Array: {nums}")
    print(f"Target index: {n}")
    
    for i in range(n - 1, -1, -1):
        max_reach = nums[i] + i
        print(f"Index {i}: value={nums[i]}, can reach index {max_reach}")
        
        if max_reach >= n:
            n = i
            print(f"  ? Updated target to index {n}")
    
    return n == 0

result = canJump_with_trace([2, 3, 1, 1, 4])
print(f"\nFinal result: {result}")
Array: [2, 3, 1, 1, 4]
Target index: 4
Index 3: value=1, can reach index 4
  ? Updated target to index 3
Index 2: value=1, can reach index 3
  ? Updated target to index 2
Index 1: value=3, can reach index 4
  ? Updated target to index 1
Index 0: value=2, can reach index 2
  ? Updated target to index 0

Final result: True

Alternative Examples

Let's test with an array where we cannot reach the end ?

class Solution:
    def canJump(self, nums):
        n = len(nums) - 1
        for i in range(n - 1, -1, -1):
            if nums[i] + i >= n:
                n = i
        return n == 0

solution = Solution()

# Test cases
test_cases = [
    [2, 3, 1, 1, 4],  # Can reach
    [3, 2, 1, 0, 4],  # Cannot reach
    [2, 0, 0],        # Cannot reach
    [1, 1, 1, 1]      # Can reach
]

for i, nums in enumerate(test_cases):
    result = solution.canJump(nums)
    print(f"Test {i+1}: {nums} ? {result}")
Test 1: [2, 3, 1, 1, 4] ? True
Test 2: [3, 2, 1, 0, 4] ? False
Test 3: [2, 0, 0] ? False
Test 4: [1, 1, 1, 1] ? True

Time Complexity

The algorithm runs in O(n) time complexity with O(1) space complexity. We iterate through the array once from right to left, making it efficient for large inputs.

Conclusion

The Jump Game uses a greedy approach working backwards from the target. By tracking the leftmost position that can reach our current target, we determine if index 0 can ultimately reach the last index.

Updated on: 2026-03-25T07:55:09+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements