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
Returns (int) 2

Example 2

Input
jumps (int[]) = [2,1,0,3]
size (int) = 4
Returns (int) -1

Example 3

Input
jumps (int[]) = [0]
size (int) = 1
Returns (int) 0

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

Ready to test your solutionRun Code checks your solution against the test cases.Shift + Enter to run

Notifications

Updates are enabled for this browser.

Receive new tutorials, articles and learning resources. You can turn updates off anytime.

Test browser code

Use this code in admin to send a test notification to this browser.