Given an array of non-negative integers
arr[]
of length
n
, where each element represents the max number of steps that can be made forward from that element. You are initially positioned at the first index of the array. Write a program to return the
minimum number of jumps to reach the last index
of the array.
Problem Note
- If it is not possible to reach the last index, return -1.
- If an element is 0, then you cannot move through that element.
Example 1
Input: arr[] = [2, 1, 1]
Output: 1
Explanation: The minimum number of jumps to reach the last index is 1. Jump 2 steps from index 0 to 2.
Example 2
Input: arr[] = [2, 3, 2, 4, 4]
Output: 2
Explanation: The minimum number of jumps to reach the last index is 2. Jump 1 step from index 0 to 1, then 3 steps to the last index.