Jump Game - Can Reach End - aloalgo

Jump Game - Can Reach End

Medium

You are given an array of integers nums. Each element nums[i] represents the maximum jump length from position i.

Your task is to determine if you can reach the last index starting from the first index.

You may assume that:

  • All elements in nums are non-negative.

Return true if the last index is reachable, false otherwise.

Example 1

Input
[2, 2, 0, 1]
Output
True
Explanation:

From index 0, you can jump to index 1 or 2. Jumping to index 1 allows you to jump to the last index (index 3). Thus, you can reach the last index.

Example 2

Input
[2, 2, 0, 0, 1]
Output
False
Explanation:

From index 0, you can jump to index 1 or 2. However, from both index 1 and 2, you cannot reach the last index (index 4) because index 3 is a dead end. Thus, you cannot reach the last index.

Example 3

Input
[0]
Output
True
Explanation:

The array has only one element, so the starting position is also the last position. You are already at the last index.

Loading...
Input
[2, 2, 0, 1]
Output
True

Hello! I am your ✨ AI assistant. I can provide you hints, explanations, give feedback on your code, and more. Just ask me anything related to the problem you're working on!