#C8997. Jump Game
Jump Game
Jump Game
You are given an array of non-negative integers \(a_0, a_1, \dots, a_{n-1}\) where each element represents the maximum number of steps that can be taken forward from that position. Determine whether it is possible to reach the last index (or beyond) starting from the first index.
More formally, starting at index 0, for each index \(i\) you can jump to any index \(j\) such that \(i < j \le i + a_i\). You need to determine whether there exists a sequence of jumps that allows you to reach index \(n-1\) or farther.
Example:
- For \(a = [2, 3, 1, 1, 4]\), you can jump from index 0 to index 1 (or 2) and eventually reach the end, so the answer is
True
. - For \(a = [3, 2, 1, 0, 4]\), you will be stuck at index 3, hence the answer is
False
.
inputFormat
The first line contains an integer \(n\) — the number of elements in the array. The second line contains \(n\) space-separated non-negative integers representing the array \(a_0, a_1, \dots, a_{n-1}\).
outputFormat
Output a single line: True
if it is possible to reach the last element (or beyond), or False
otherwise.
5
2 3 1 1 4
True