Minimum number of jumps | Practice | GeeksforGeeks

Given an array of N integers arr[] where each element represents the max number of steps that can be made forward from that element. Find the minimum number of jumps to reach the end of the array (starting from the first element). If an element is 0, then you cannot move through that element.

Note: Return -1 if you can't reach the end of the array.

Example 1: 
Input:
N = 11 
arr[] = {1, 3, 5, 8, 9, 2, 6, 7, 6, 8, 9} 
Output: 3 
Explanation: 
First jump from 1st element to 2nd 
element with value 3. Now, from here 
we jump to 5th element with value 9, 
and from here we will jump to the last. 
Example 2:
Input :
N = 6
arr = {1, 4, 3, 2, 6, 7}
Output: 2 
Explanation: 
First we jump from the 1st to 2nd element 
and then jump to the last element.


Your task:
You don't need to read input or print anything. Your task is to complete function minJumps() which takes the array arr and it's size N as input parameters and returns the minimum number of jumps. If not possible return -1.

Expected Time Complexity: O(N)
Expected Space Complexity: O(1)

Constraints:
1 ≤ N ≤ 107
0 ≤ arri ≤ 107
0
parkarshreyas7718 hours ago
//Efficient JAVA Solution
class Solution{
    static int minJumps(int[] arr){
         int i = 0;
        
        int n = arr.length;
       
        if((n==0) || (n==1)){
            return 0;
        }
        
       
       int nextStep = 0;
        int step = 0;
        int jump = 0;
        while(i<n-1){
            
        step = Math.max(step,i+arr[i]);
                if(i == nextStep){
                    nextStep = step;
                    jump++;
                }
                if( nextStep >= arr.length-1 ) return jump; 
                
            i++;
         
        }
        return -1;
            
    }
}
0
dipanshusharma93131 day ago
// java solution
class Solution{   static int minJumps(int[] arr){       // your code here       int ans = -1;       if(arr.length == 1){           return 0;       }       if(arr[0] == 0){           return ans;       }       int maxrange = arr[0], step = 0, jumps = 0;       for(int i=0;i<arr.length;i++){           step = Math.max(step,i+arr[i]);           if(i == maxrange){               maxrange = step;               jumps++;           }           if(maxrange >=arr.length-1){               return jumps+1;           }       }       return ans;   }}
0
badgujarsachin831 day ago
int minJumps(int arr[], int n){
        //  code here
        if(n<=1){
            return 0;
        }else if(arr[0]==0){
            return -1;
        }else{
            int jump=0,position=0,range=0;
            for(int i=0;i<n-1;i++){
                range=max(range,i+arr[i]);
                if(i==position){
                    jump++;
                    if(i>=range){
                        return -1;
                    }
                    position=range;
                }
            }
            return jump;
        }
     }
0
ashfaqhussainmd152 days ago
C++ Easy and Simple Approach
 
int minJumps(int arr[], int n){       // Your code here              if(n<=1)       {           return 0;       }              if(arr[0]==0)   return -1;              int jump=0;       int position=0;       int maxReach=0;              for(int i=0;i<n-1;i++)       {           maxReach=max(maxReach,i+arr[i]);                      if(i==position)           {               jump++;                              if(i>=maxReach) return -1;                              position=maxReach;           }       }              return jump;   }
-2
ankitabxrt2 days ago
What is compilation error 😐
0
vijethareddykasala3 days ago
class Solution{ public:   int minJumps(int arr[], int n){       int jump;   if(n>1 && arr[0]!=0)     jump=arr[0];   else if(n==1 && arr[0]==0)     return 0;     else     return -1;   int maxreach=arr[0];   int count=1;       for(int i=1;i<n;i++){           jump--;           maxreach=max(maxreach,arr[i]+i);            if(i==n-1)                 return count;           if(jump==0 && i<maxreach){               count++;           jump=maxreach-i;          }          if(i>=maxreach)   return -1;                          }   }};
+1
vaibhavkhanna8bp3 days ago
can anybody please tell where is this solution failing it is passing 138/141 cases dont know where it is failing 
class Solution{public:   int minJumps(int a[], int n)   {       int count = 0, i = 0;       while (i < n)       {           if (a[i] == 0)           {               if (i != n - 1)               {                   return -1;               }               else               {                   return count;               }           }           if (i + a[i] >= n - 1)           {               count++;               return count;           }           int current = i + 1;           int iterator = 1;           int nextMaxIndex = current;           int nextMax = INT_MIN;           while (iterator <= a[i])           {               if (current + a[current] >= n - 1)               {                   return count + 2;               }               else if (a[current] >= nextMax)               {                   nextMaxIndex = current;                   nextMax = a[current];               }               current++;               iterator++;           }           count++;           i = nextMaxIndex;           // cout << "Taken " << count << " steps and reached at index : " << nextMaxIndex << endl;       }   }};
+1
amobala984 days ago
JAVA GREEDY SOLUTION : 
 
class Solution{
    static int minJumps(int[] arr){
     int ans= -1;
     if( arr.length == 0 ) return ans;
     if( arr.length==1 ) return 0;
     int maxReach = arr[0], jump = 0 , val = 0; 
     for( int i = 0 ; i < arr.length ; i++){
         val = Math.max( val, i+arr[i]); 
         if( i == maxReach)
             {   maxReach = val;
                 jump++;
             }
        if( maxReach >= arr.length-1 ) return jump+1;         
     }
     return ans;
    }
}
+2
rathodumang3196 days ago
Java Solution:
class Solution{
    static int minJumps(int[] arr){
        int maxR = arr[0];
        int step = arr[0];
        int jump=1;
        int n = arr.length;
        // Base case
        if(arr.length==1 && arr[0]==0){
            return 0;
        }
        else if(arr[0]==0){
            return -1;
        }
        else if(arr.length==1){
            return 0;
        }
        
        // ************************************ //
        for(int i=1; i<n; i++){
            if(i==n-1){
                return jump;
            }
            maxR = Math.max(maxR, i+arr[i]);
            step--;
            if(step==0){
                jump++;
                if(i>=maxR){
                    return -1;
                }
                step = maxR - i;
            }
        }
        return jump;
    }
}
 
+1
chetandas20011 week ago
Can someone please explain what's wrong in this code... I am getting segmentation fault
int help(int i,int n,int a[],vector<int> &dp)   {       if(i>=n-1)           return 0;       if(a[i]==0)           return 1e8;       if(dp[i]!=-1)           return dp[i];       else       {           int minjmps=1e8;           for(int k=1;k<=a[i];k++)           {               int temp=1+help(i+k,n,a,dp);               minjmps=min(minjmps,temp);           }           return dp[i]=minjmps;       }   }   int minJumps(int a[], int n){       // Your code here       if(n==1)         return 0;       else if(a[0]==0)        return -1;       vector<int> dp(n,-1);       int ans=help(0,n,a,dp);       if(ans>=1e8)        return -1;       return ans;   }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested 
                                    against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code.
                                    On submission, your code is tested against multiple test cases consisting of all
                                        possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as
                                        the final solution code.
You can view the solutions submitted by other users from the submission tab.
Make sure you are not using ad-blockers.
Disable browser extensions.
We recommend using latest version of your browser for best experience.
Avoid using static/global variables in coding problems as your code is tested 
                                against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases in coding problems does not guarantee the 
                                correctness of code. On submission, your code is tested against multiple test cases
                                    consisting of all possible corner cases and stress constraints.