MCPcopy Create free account
hub / github.com/Tiwarishashwat/InterviewCodes / minJumps

Method minJumps

Minimum_number_of_jumps.java:2–26  ·  view source on GitHub ↗
(int[] arr)

Source from the content-addressed store, hash-verified

1class Solution{
2 static int minJumps(int[] arr){
3 // your code here
4 if(arr.length==1)
5 return 0;
6
7 if(arr[0]==0)
8 return -1;
9
10 int jump=0,halt=0;
11 int maxDist=Integer.MIN_VALUE;
12 for(int i=0;i<arr.length-1;i++)
13 {
14 if(arr[i]+i>=maxDist)
15 maxDist=arr[i]+i;
16
17 if(i==halt)
18 {
19 halt=maxDist;
20 jump++;
21 if(halt>=arr.length-1)
22 break;
23 }
24 }
25 return (halt>=arr.length-1)?jump:-1;
26 }
27}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected