#C1561. Minimum Taps to Reach the End
Minimum Taps to Reach the End
Minimum Taps to Reach the End
You are given an array of non-negative integers of length \(n\). Each element of the array represents the maximum number of steps you can move forward from that index. Your task is to determine the minimum number of taps (jumps) needed to reach the last element of the array. If the end of the array cannot be reached, output \(-1\).
The problem can be viewed as a variation of the classical jump game. For an array \(a[0 \ldots n-1]\), when you are at index \(i\), you can jump to any index \(j\) where \(i+1 \leq j \leq i+a[i]\). The goal is to reach index \(n-1\) with the minimum number of jumps.
Note: If the array contains only one element, you are already at the end, so the answer is \(0\). Otherwise, if at any point you cannot move forward further, you should output \(-1\).
inputFormat
The input is read from standard input (stdin) and consists of two lines:
- The first line contains a single integer \(n\) (the number of elements in the array).
- The second line contains \(n\) space-separated non-negative integers representing the array.
outputFormat
Output a single integer to standard output (stdout) which is the minimum number of taps needed to reach the last element. If it is not possible, output \(-1\).
## sample5
2 3 1 1 4
2
</p>