#C3077. Minimum Jumps to Reach the End

    ID: 46464 Type: Default 1000ms 256MiB

Minimum Jumps to Reach the End

Minimum Jumps to Reach the End

You are given an array of non‐negative integers where each element represents the maximum jump length from that position. The task is to determine the minimum number of jumps required for a frog to reach the end of the array. If it is not possible for the frog to reach the last element, return -1.

Formally, given an array \(a_0, a_1, \dots, a_{n-1}\), you need to find the minimum number of jumps such that:

\[ jump\_count = \min \{ k \mid \exists i_0=0, i_1, \dots, i_k = n-1 \text{ with } i_{j+1} \le i_j + a_{i_j} \} \]

If no such sequence exists, output -1.

inputFormat

The first line contains an integer \(n\) representing the number of elements in the array. The second line contains \(n\) space-separated non-negative integers, where the \(i^{th}\) integer represents \(a_i\), the maximum jump length from that position.

Input is read from standard input (stdin).

outputFormat

Output a single integer representing the minimum number of jumps needed to reach the last element of the array. If it is impossible, output -1.

Output is written to standard output (stdout).

## sample
5
2 3 1 1 4
2

</p>