#K94067. Jump Game - Reach the End?
Jump Game - Reach the End?
Jump Game - Reach the End?
Given an array of non-negative integers, where each element represents the maximum jump length from that position, determine if you can reach the last index starting from the first index. At each position \( i \), you can jump any distance from 1 up to \( arr[i] \).
This can be approached with a greedy algorithm. Maintain a variable \( maxReach \) which is the farthest index reachable so far. At each index \( i \), if \( i \) is greater than \( maxReach \), then the current position is unreachable. Otherwise, update \( maxReach \) to \( \max(maxReach, i + arr[i]) \). If \( maxReach \) reaches or exceeds the last index (i.e. \( n-1 \)), then the answer is True; otherwise, it is False.
inputFormat
The first line of input contains an integer \( n \) representing the number of elements in the array. The second line contains \( n \) non-negative integers separated by spaces, representing the array.
outputFormat
Output True
if it is possible to reach the last index, otherwise output False
. The output is case-sensitive and should be printed on a single line.
5
2 3 1 1 4
True
</p>