You stand on cell 0 of a straight track of n cells. Cell i holds a non-negative integer a[i]: from cell i you may jump forward to any cell in the range i+1 through i+a[i] (not past the end of the track). A value of 0 means you cannot move from that cell.
Decide whether you can reach the last cell, index n-1.
Line 1: an integer n.
Line 2: n space-separated non-negative integers a[0..n-1] (present whenever n >= 1).
Print YES if the last cell is reachable from cell 0, otherwise NO.
Example 1
Input
5 2 3 1 1 4
Expected
YES
Explanation
From cell 0 (jump up to 2) reach cell 1; from cell 1 (jump up to 3) reach cell 4, the last cell. So YES.
Example 2
Input
5 3 2 1 0 4
Expected
NO
Explanation
The furthest you can reach is cell 3, whose value 0 stops all movement. Cell 4 is never reachable, so NO.
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 →