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 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 returns -1.

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

Constraints:
1 ≤ N ≤ 107
0 ≤ arri ≤ 107
0
shettyrohan183
This comment was deleted.
0
venkatesh17nayak14 hours ago
Test case: 
102 3 1 1 2 4 2 0 1 1
 
expected output = 4
 
my output = 6
 
How is result = 4, shouldn't it be 6 ?
2→1→1→2→2→1→1(end)
0
aryangrover2304023 days ago
Can anyone help why this solution failing?
 
 
static int minJumps(int[] arr) {
       int n = arr.length;
       int[] dp = new int[n];
       dp[n-1] = 0;
       
       for(int i=n-2; i>=0; i--) {
           int min = Integer.MAX_VALUE;
           for(int jump=1; jump<=arr[i] && jump+i<n; jump++) {
               min = Math.min(min, dp[i+jump]);
           }
           dp[i] = (min==Integer.MAX_VALUE)?min:min+1;
       }
       return dp[0]==Integer.MAX_VALUE?-1:dp[0];
   }
 
 
0
sansan23 days ago
Can someone help me with this code?Its failing due to time....
 
 int i=0,temp,j=0;       while(i<arr.length)       {           if(arr[i]>0)           {               temp=arr[i];               i+=temp;               j+=1;           }                  }       if(j>0)           return j;       else           return -1;
 
 
0
ayush63784 days ago
Recursive solution
 
class Solution:def minJumps(self, arr, n):    if len(arr) <= 1:        return 0    if arr[0] == 0:        return -1     if len(arr) - arr[0] - 1 <= 0:         return 1     max_i = 1     max_jum = n - 2 - arr[1]     for i in range(1, arr[0] + 1):           if n - i - 1 - arr[i] < max_value:               max_i = i               max_jump = n - i - 1 - arr[i]        rec = self.minJumps(arr[max_i:], n - max_i)     if rec == -1:           return -1     ans = 1 + rec     return ans
0
nitishpescse5 days ago
// { Driver Code Starts#include<bits/stdc++.h>using namespace std;
// } Driver Code Ends// Function to return minimum number of jumps to end of array
class Solution{ public:   int minJumps(int arr[], int n) {       if (n == 1)           return 0;       if (arr[0] == 0)           return -1;       int maxRange = arr[0], steps = arr[0], jumps = 0;       for (int i = 1; i < n - 1; i++) {           steps--;           maxRange = max(maxRange, arr[i] + i);           if (steps == 0) {               jumps++;               steps = maxRange - i;               if (steps <= 0)                   return -1;           }       }       return jumps + 1;   }};
// { Driver Code Starts.
int main(){   int t;   cin>>t;   while(t--)   {       int n,i,j;       cin>>n;       int arr[n];       for(int i=0; i<n; i++)           cin>>arr[i];       Solution obj;       cout<<obj.minJumps(arr, n)<<endl;   }   return 0;} // } Driver Code Ends
0
jy9041476 days ago
java solution by jitu yadav
 static int minJumps(int[] arr){       int n=arr.length;
if(n==1)return 0;if(arr[0]==0)return -1;if(arr[0]>n)return 1;int maxreach=arr[0];int step=arr[0];int jump=1;for(int i=1;i<n;i++){   if(i==n-1)return jump;   maxreach=Math.max(maxreach,i+arr[i]);   step--;   if(step==0){       jump++;       if(i>=maxreach)return -1;       step=maxreach-i;   }}return -1;
}
+1
buchadebnath1 week ago
Can someone explain me the error of this code for 
 
TestCase:-
 
5
0 1 1 1 1
 
CODE:-
 
class Solution{   static int minJumps(int[] arr){       int len=arr.length;       int[] jump=new int[len];       jump[0]=0;       for(int i=1;i<len;i++){           jump[i]=Integer.MAX_VALUE;           for(int j=0;j<i;j++){                              if(arr[j]+j>=i && arr[j]!=Integer.MAX_VALUE){                   jump[i]=Math.min(jump[i],jump[j]+1);               }           }       }       if(jump[len-1]!=Integer.MAX_VALUE)           return jump[len-1];       else           return -1;   }}
+2
milindprajapatmst191 week ago
class Solution {
  public:
    int minJumps(int arr[], int n) {
        if (n == 1)
            return 0;
        if (arr[0] == 0)
            return -1;
        int maxRange = arr[0], steps = arr[0], jumps = 0;
        for (int i = 1; i < n - 1; i++) {
            steps--;
            maxRange = max(maxRange, arr[i] + i);
            if (steps == 0) {
                jumps++;
                steps = maxRange - i;
                if (steps <= 0)
                    return -1;
            }
        }
        return jumps + 1;
    }
};
+2
0xqt6eumuntdjmlbvx8kwrxi8g36mhs98c0r12mw2 weeks ago
A fun Python recursive solution :)
 
class Solution:
   def recShortList(self, arr, count, n):
    arrLen = len(arr)
    if arrLen < 2 :#and arr[0] > 0:
     return count
   
    for i in range(arrLen-1):
     if arr[i] >= (arrLen - (i+1)) and i < arrLen:
      return self.recShortList(arr[0:i+1],count + 1,n)
    return -1

def minJumps(self, arr, n):
    return self.recShortList(arr,0,n)
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.