Skip to content
AI360Xpert
Beta
Difficulty: MediumGreedy

Jump Game

Problem in Plain English

Given an array where each element caps how far you can jump forward from that position, decide whether the last index is reachable starting from the first index.

Problem Statement

Given a nonempty integer array nums, start at index 0. From index i, you may jump forward by any number of positions from 1 through nums[i]; the value is a maximum, not a required distance. Return whether you can reach the last index. You need not return a path or minimize the number of jumps.

Constraints

  • 1≤n=nums.length≤1041 \leq n = \mathrm{nums.length} \leq 10^4.
  • 0≤nums[i]≤1050 \leq \mathrm{nums}[i] \leq 10^5.

Examples

Example 1

Input
nums = [2, 3, 1, 1, 4]
Output
true

Jump one step from index 0 to 1, then three steps to index 4. The first jump need not use the full value 2.

Example 2

Input
nums = [3, 2, 1, 0, 4]
Output
false

Indices 0, 1, and 2 can reach at most index 3. Its zero cannot extend reachability, so index 4 stays unreachable.

Example 3

Input
nums = [0]
Output
true

The starting index is already the last index; no jump is required even though the only value is zero.

Example 4

Input
nums = [2, 0, 0]
Output
true

Index 0 jumps directly to index 2, skipping the zero at index 1. A zero alone does not imply failure.

Intuition

Reachable positions form a continuous prefix because any jump distance up to the maximum is allowed. In Example 1, index 0 with value 2 makes indices 1 and 2 reachable; reachable index 1 then extends that prefix through index 4. We can either explore the choices and remember each result, or merge every reachable choice into one farthest frontier. A zero blocks progress only when no earlier reachable position can jump past it.

Approaches

Memoized Recursion

Solution Details

Reveal Memoized Recursion: intuition, complexity, and code

Hints

Hint 1
Ask what single number summarizes everything the prefix of the array can do.
Hint 2
Decide what it means when the current index lies beyond that summary.
Hint 3
Update the summary with each index before testing whether the end is covered.

Edge Cases

  • Single element [0], which starts on the goal and returns true.
  • A zero that can be skipped, as in [2, 0, 0], which still returns true.
  • A zero wall before the end, as in [3, 2, 1, 0, 4], which returns false.

Common Mistakes and Interview Tips

  • Using a jump from an unreachable index; [0, 100000] must return false. Check the gap before extending the frontier.
  • Treating every zero as failure: [2, 0, 0] succeeds because an earlier jump bypasses it.
  • Assuming every jump must use the full value. The value is an upper bound, and shorter landings are legal.
  • Using native recursion on a long one-step chain can overflow Python, JavaScript or Java call stacks. Explicit frames retain the memoized search without that runtime limit.

Key Takeaway

When reachability only expands, maintain the frontier as one number and let each position extend it.