#C11068. Minimum Number of Jumps

    ID: 40343 Type: Default 1000ms 256MiB

Minimum Number of Jumps

Minimum Number of Jumps

You are given an array A of non-negative integers where each element represents the maximum number of steps that can be jumped forward from that element. The 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 end, output -1.

Note: When the array has only one element, you are already at the destination, so the answer is 0.

The problem can be thought of in terms of a greedy algorithm. Maintain variables for the maximum index reachable, the range of the current jump, and the count of jumps. Each time you reach the end of the current jump range, increment the jump count and update the range to the maximum reach possible.

The underlying principle follows a greedy strategy and the time complexity is linear, i.e., \(O(n)\).

inputFormat

The input is given via stdin and consists of two lines:

  1. The first line contains a single integer n, the number of elements in the array.
  2. The second line contains n space-separated non-negative integers representing the array.

Constraints: \(1 \leq n \leq 10^5\); Each element of the array is between 0 and \(10^5\).

outputFormat

The output should be printed to stdout as a single integer representing the minimum number of jumps required to reach the last index of the array. If it is not possible to reach the end, print -1.

## sample
5
2 3 1 1 4
2

</p>