#K63152. Can Capture All Flags?
Can Capture All Flags?
Can Capture All Flags?
You are given a list of non-negative integers representing the maximum number of steps that can be jumped from that position. Starting from the first position, your goal is to determine if you can reach the last position in the list.
Formally, let the array be denoted by \(A\) with length \(n\). At each index \(i\), you can jump at most \(A[i]\) steps forward. You need to determine whether it is possible to reach the last position (i.e., index \(n-1\)). The underlying recurrence can be described as:
[ \text{maxReach} = \max(\text{maxReach}, i + A[i]) ]
If at any index \(i\), \(i\) is greater than the current \(maxReach\), it is impossible to proceed, and the answer is False. Otherwise, if you can update \(maxReach\) so that it is at least \(n-1\), the answer is True.
Note that if the input list is empty, the answer is False since there is no position to reach.
inputFormat
The input is read from standard input (stdin) and consists of two lines:
- The first line contains a single integer \(n\) (\(n \ge 0\)), representing the length of the list.
- The second line contains \(n\) space-separated non-negative integers representing the list \(A\), where each integer denotes the maximum jump length from that index.
outputFormat
Output a single line to standard output (stdout) containing either True
or False
, indicating whether it is possible to reach the last index of the array.
5
2 3 1 1 4
True