Back to ArraysJump Game
Medium

Jump Game

MediumMust DoGreedy Array Patterns

Given an array where each element represents your maximum jump length from that position, determine whether you can reach the last index starting from the first.

Examples

Example 1

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

Explanation: Jump 1 step from index 0 to 1, then 3 steps from index 1 to the last index (4).

Example 2

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

Explanation: Every path leads to index 3, whose value is 0 — from there, index 4 is unreachable.

Example 3

Input:
nums = [0]
Output:
true

Explanation: A single-element array starts already at the last index, so no jump is needed.

Constraints

  • 1 <= nums.length <= 10^4
  • 0 <= nums[i] <= 10^5

Loading editor…

Code execution is coming soon.