#K33577. Minimum Jumps to End

    ID: 25118 Type: Default 1000ms 256MiB

Minimum Jumps to End

Minimum Jumps to End

Given an array of nonnegative integers, each element represents the maximum jump length from that position. Your task is to determine the minimum number of jumps required to reach the last index starting from the first index. If it is not possible to reach the end, output -1.

The problem can be understood mathematically as: Given an array arr of length n, find the minimum number of jumps such that starting at index 0, you eventually reach index n-1. If at some position, no further progress can be made (i.e. arr[i] = 0 when i < n-1), then reaching the end is impossible.

The greedy strategy is often used here. At each step, update the furthest reachable index using the formula \(max\_reach = \max_{i}(i + arr[i])\) and decide when to take the next jump.

inputFormat

The input is read from standard input (stdin) and consists of two lines:

  • The first line contains a single integer n representing the number of elements in the array.
  • The second line contains n space-separated integers, where each integer is the maximum jump length from that index.

outputFormat

Output a single integer to standard output (stdout) representing the minimum number of jumps required to reach the last index. If it is not possible to reach the end, output -1.

## sample
5
2 3 1 1 4
2