#C6779. Minimum Jumps to End of Array

    ID: 50576 Type: Default 1000ms 256MiB

Minimum Jumps to End of Array

Minimum Jumps to End of Array

You are given an array of non-negative integers. Each element of the array represents the maximum jump length at that position. Your task is to determine the minimum number of jumps required to reach the last index of the array. If it is not possible to reach the last index, output -1.

Mathematical Model:

Let \( arr \) be an array of length \( n \). You must compute the minimal \( j \) such that by taking \( j \) jumps, you can reach index \( n-1 \), otherwise output -1.

Example:

  • Input: [2, 3, 1, 1, 4] → Output: 2
  • Input: [1, 0, 2, 3, 1] → Output: -1

Note: The first element of the array is the maximum number of steps you can take from the first position.

inputFormat

The first line of input contains an integer \( n \), representing the number of elements in the array. The second line contains \( n \) space-separated non-negative integers which are the elements of the array.

outputFormat

Output a single integer: the minimum number of jumps required to reach the last index of the array, or \( -1 \) if it is not possible.

## sample
5
2 3 1 1 4
2