Proficient Level

Implement the method minJumpsToEnd that returns the fewest jumps needed to reach the last index.

Each array value tells the maximum number of positions you may jump forward from that index.

The task is designed to test careful handling of edge cases, not only the most common input.

  • Use only the first size elements.
  • Return 0 when size is 1.
  • Return -1 if the end cannot be reached.
Example 1
Input:
jumps (int[]) = [2,3,1,1,4]
size (int) = 5
Return:
(int) 2
Example 2
Input:
jumps (int[]) = [2,1,0,3]
size (int) = 4
Return:
(int) -1
Example 3
Input:
jumps (int[]) = [0]
size (int) = 1
Return:
(int) 0

Track the current jump range and farthest reachable index. When the current range ends, take one jump.

Run your code to see the result.