#K13456. Minimum Jumps to End
Minimum Jumps to End
Minimum Jumps to End
You are given a sequence of non-negative integers, where each integer represents the maximum length of a jump you can make from that position. Your task is to determine the minimum number of jumps required to reach the end of the sequence. If it is not possible to reach the end, return -1
. In addition, if the input is not a valid sequence of non-negative integers, output Invalid input
.
Formally, given an array \(a[0\dots n-1]\) where each \(a[i]\) is a non-negative integer, find the minimum number of jumps needed to go from index 0 to index \(n-1\). If it is impossible to reach the end, output -1
. If the input is malformed, output Invalid input
.
Note: All formulas are written in \(\LaTeX\) format. For example, the jump condition from index \(i\) to index \(j\) can be expressed as \(j \leq i + a[i]\).
inputFormat
The input is provided via stdin as a single line containing space-separated tokens. Each token should represent a non-negative integer. An empty input represents an empty list.
outputFormat
The output should be printed to stdout. It is either an integer representing the minimum number of jumps required to reach the end of the list, or -1
if the end cannot be reached. If the input is invalid, output Invalid input
.
2 3 1 1 4
2
</p>