#K96. Minimum Jumps to Reach the End

    ID: 38988 Type: Default 1000ms 256MiB

Minimum Jumps to Reach the End

Minimum Jumps to Reach the End

You are given a sequence of non-negative integers where each integer represents the maximum jump length from that position. Starting from the first index, your task is to determine the minimum number of jumps required to reach the last index of the sequence. If it is impossible to reach the end, output -1.

Formally, given an array \(A = [a_0, a_1, \dots, a_{n-1}]\) where each \(a_i\) indicates the maximum jump length at position \(i\), you need to find the minimum number of jumps to reach index \(n-1\) from index 0. At any index \(i\), you can jump to any index \(j\) such that \(i < j \leq i + a_i\).

inputFormat

The input is provided via standard input (stdin). The first line contains a single integer (n) (1 ≤ n ≤ 10^5) denoting the length of the sequence. The second line contains (n) space-separated non-negative integers which form the sequence.

outputFormat

Output a single integer to standard output (stdout) representing the minimum number of jumps required to reach the end of the sequence. If the end is not reachable, output -1.## sample

5
2 3 1 1 4
2