#K5161. Minimum Jump Game

    ID: 29126 Type: Default 1000ms 256MiB

Minimum Jump Game

Minimum Jump Game

You are given an array of non‐negative integers where each element represents the maximum jump length from that position. Your task is to determine the minimum number of jumps required to reach the end of the array. If the end is not reachable, output -1.

For example, given the array [2, 3, 1, 1, 4], the minimum number of jumps is 2 because you can jump from index 0 to index 1 and then from index 1 to index 4.

At each index i, the maximum reachable index is given by $$i + a_i$$ where (a_i) is the value at index i. Use this to decide the optimal jump at every step.

inputFormat

The input is given via standard input (stdin). The first line contains a single integer N, representing the number of elements in the array. The second line contains N space-separated integers which represent the array elements.

outputFormat

Output a single integer via standard output (stdout) that represents the minimum number of jumps required to reach the end of the array, or -1 if it is not possible.## sample

5
2 3 1 1 4
2