A line of n boulders is numbered 0 to n-1. Standing on boulder i, a hiker can leap forward to any boulder j with i < j <= i + a_i, where a_i is the maximum leap distance printed on boulder i (a hiker never leaps backward or off the end of the line). Starting on boulder 0, determine whether the hiker can reach boulder n-1.
Line 1: an integer n.
Line 2: n space-separated integers a_0 a_1 ... a_{n-1}.
Print YES if boulder n-1 is reachable from boulder 0, otherwise print NO.
Example 1
Input
5 2 3 1 1 4
Expected
YES
Explanation
From boulder 0 leap to boulder 1 (max leap 2), then from boulder 1 leap 3 to reach boulder 4, the last one. Reachable.
Example 2
Input
4 1 0 1 0
Expected
NO
Explanation
From boulder 0 the hiker can only reach boulder 1, whose leap distance is 0, so the hiker is stuck at boulder 1 and can never reach boulder 3.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →