Minimum number of jumps to reach end | Set 2 (O(n) solution)
Given an array of integers where each element represents the max number of steps that can be made forward from that element. Write a function to return the minimum number of jumps to reach the end of the array (starting from the first element). If an element is 0, then we cannot move through that element. If we can’t reach the end, return -1.
Examples:
Input: arr[] = {1, 3, 5, 8, 9, 2, 6, 7, 6, 8, 9} Output: 3 (1-> 3 -> 8 -> 9) Explanation: Jump from 1st element to 2nd element as there is only 1 step, now there are three options 5, 8 or 9. If 8 or 9 is chosen then the end node 9 can be reached. So 3 jumps are made.
Input : arr[] = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1} Output : 10 Explanation: In every step a jump is needed so the count of jumps is 10.
In this post, its O(n) solution will be discussed.
Algorithm:
- First we initialize the three variable and that variable name is ‘max_reach,’ ‘steps’, and ‘last_jump’
- Then we Set the ‘max_reach’ to the first element of the array, ‘steps’ to 0, and ‘last jump’ to 0.
- Then Iterate the array from the index 1 to the end, and updating the ‘max_reach’ to the maximum index that can be reached from the current index using the jump size specified by the value at the current index.
- If our current index is equal to the ‘last_jump’ that means we have reached the end of the current_jump, then we update the ‘last_jump’ to ‘max_reach’ and increment in the ‘steps’.
- If ‘last_jump’ is going to be greater then or equal to the last index in the array, we have reached the end and return ‘steps’.
- If at the end of the loop ‘last_jump’ is less than the last index in the array, we cannot reach the end and return -1.
In Set -1, O(n2) solution is discussed.
Implementation: Variables to be used:
- maxReach The variable maxReach stores at all times the maximal reachable index in the array.
- jump stores the amount of jumps necessary to reach the maximal reachable position. It also indicates the current jump we are making in the array.
- step The variable step stores the number of steps we can still take in the current jump ‘jump’ (and is initialized with value at index 0, i.e. initial number of steps)
Given array arr = 1, 3, 5, 8, 9, 2, 6, 7, 6, 8, 9
- maxReach = arr[0]; // arr[0] = 1, so the maximum index we can reach at the moment is 1.
step = arr[0]; // arr[0] = 1, the amount of steps we can still take is also 1.
jump = 1; // we are currently making our first jump.
Now, starting iteration from index 1, the above values are updated as follows:
- First, we test whether we have reached the end of the array, in that case, we just need to return the jump variable.
if (i == arr.length - 1) return jump;
- Next we update the maxReach. This is equal to the maximum of maxReach and i+arr[i](the number of steps we can take from the current position).
maxReach = Math.max(maxReach, i+arr[i]);
- We used up a step to get to the current index, so steps has to be decreased.
step--;
- If no more steps are remaining (i.e. steps=0, then we must have used a jump. Therefore increase jump. Since we know that it is possible somehow to reach maxReach, we again initialize the steps to the number of steps to reach maxReach from position i. But before re-initializing step, we also check whether a step is becoming zero or negative. In this case, It is not possible to reach further.
if (step == 0) { jump++; if(i>=maxReach) return -1; step = maxReach - i; }
Implementation:
C++
// C++ program to count Minimum number // of jumps to reach end #include <bits/stdc++.h> using namespace std; int max( int x, int y) { return (x > y) ? x : y; } // Returns minimum number of jumps // to reach arr[n-1] from arr[0] int minJumps( int arr[], int n) { // The number of jumps needed to // reach the starting index is 0 if (n <= 1) return 0; //If value of first index guarantees //only 1 jump is needed, return 1 if (arr[0] >= n-1) return 1; // Return -1 if not possible to jump if (arr[0] == 0) return -1; // initialization // stores all time the maximal // reachable index in the array. int maxReach = arr[0]; // stores the number of steps // we can still take int step = arr[0]; // stores the number of jumps // necessary to reach that maximal // reachable position. int jump = 1; // Start traversing array int i = 1; for (i = 1; i < n; i++) { // Check if we have reached the end of the array if (i == n - 1) return jump; //Check if value at current index guarantees jump to end if (arr[i] >= (n-1) - i) return jump + 1; // updating maxReach maxReach = max(maxReach, i + arr[i]); // we use a step to get to the current index step--; // If no further steps left if (step == 0) { // we must have used a jump jump++; // Check if the current index/position or lesser index // is the maximum reach point from the previous indexes if (i >= maxReach) return -1; // re-initialize the steps to the amount // of steps to reach maxReach from position i. step = maxReach - i; } } return -1; } // Driver program to test above function int main() { int arr[] = { 1, 3, 5, 8, 9, 2, 6, 7, 6, 8, 9 }; int size = sizeof (arr) / sizeof ( int ); // Calling the minJumps function cout << ( "Minimum number of jumps to reach end is %d " , minJumps(arr, size)); return 0; } // This code is contributed by // Shashank_Sharma |
C
// C program to count Minimum number // of jumps to reach end #include <stdio.h> int max( int x, int y) { return (x > y) ? x : y; } // Returns minimum number of jumps // to reach arr[n-1] from arr[0] int minJumps( int arr[], int n) { // The number of jumps needed to // reach the starting index is 0 if (n <= 1) return 0; //If value of first index guarantees //only 1 jump is needed, return 1 if (arr[0] >= n-1) return 1; // Return -1 if not possible to jump if (arr[0] == 0) return -1; // initialization // stores all time the maximal // reachable index in the array. int maxReach = arr[0]; // stores the number of steps // we can still take int step = arr[0]; // stores the number of jumps // necessary to reach that maximal // reachable position. int jump = 1; // Start traversing array int i = 1; for (i = 1; i < n; i++) { // Check if we have reached the end of the array if (i == n - 1) return jump; //Check if value at current index guarantees jump to end if (arr[i] >= (n-1) - i) return jump + 1; // updating maxReach maxReach = max(maxReach, i + arr[i]); // we use a step to get to the current index step--; // If no further steps left if (step == 0) { // we must have used a jump jump++; // Check if the current index/position or lesser index // is the maximum reach point from the previous indexes if (i >= maxReach) return -1; // re-initialize the steps to the amount // of steps to reach maxReach from position i. step = maxReach - i; } } return -1; } // Driver program to test above function int main() { int arr[] = { 1, 3, 5, 8, 9, 2, 6, 7, 6, 8, 9 }; int size = sizeof (arr) / sizeof ( int ); // Calling the minJumps function printf ( "Minimum number of jumps to reach end is %d " , minJumps(arr, size)); return 0; } // This code is contributed by Abhishek Kumar Singh |
Java
// Java program to count Minimum number // of jumps to reach end class Test { static int minJumps( int arr[]) { if (arr.length <= 1 ) return 0 ; //If value of first index guarantees //only 1 jump is needed, return 1 if (arr[ 0 ] >= arr.length- 1 ) return 1 ; // Return -1 if not possible to jump if (arr[ 0 ] == 0 ) return - 1 ; // initialization int maxReach = arr[ 0 ]; int step = arr[ 0 ]; int jump = 1 ; // Start traversing array for ( int i = 1 ; i < arr.length; i++) { // Check if we have reached // the end of the array if (i == arr.length - 1 ) return jump; //Check if value at current index guarantees jump to end if (arr[i] >= (arr.length- 1 ) - i) return jump + 1 ; // updating maxReach maxReach = Math.max(maxReach, i + arr[i]); // we use a step to get to the current index step--; // If no further steps left if (step == 0 ) { // we must have used a jump jump++; // Check if the current // index/position or lesser index // is the maximum reach point // from the previous indexes if (i >= maxReach) return - 1 ; // re-initialize the steps to the amount // of steps to reach maxReach from position i. step = maxReach - i; } } return - 1 ; } // Driver method to test the above function public static void main(String[] args) { int arr[] = new int [] { 1 , 3 , 5 , 8 , 9 , 2 , 6 , 7 , 6 , 8 , 9 }; // calling minJumps method System.out.println(minJumps(arr)); } } |
Python3
# python program to count Minimum number # of jumps to reach end # Returns minimum number of jumps to reach arr[n-1] from arr[0] def minJumps(arr, n): # The number of jumps needed to reach the starting index is 0 if (n < = 1 ): return 0 #If value of first index guarantees only 1 jump is needed, return 1 if (arr[ 0 ] > = n - 1 ): return 1 # Return -1 if not possible to jump if (arr[ 0 ] = = 0 ): return - 1 # initialization # stores all time the maximal reachable index in the array maxReach = arr[ 0 ] # stores the amount of steps we can still take step = arr[ 0 ] # stores the amount of jumps necessary to reach that maximal reachable position jump = 1 # Start traversing array for i in range ( 1 , n): # Check if we have reached the end of the array if (i = = n - 1 ): return jump #Check if value at current index guarantees jump to end if (arr[i] > = (n - 1 ) - i): return jump + 1 # updating maxReach maxReach = max (maxReach, i + arr[i]) # we use a step to get to the current index step - = 1 ; # If no further steps left if (step = = 0 ): # we must have used a jump jump + = 1 # Check if the current index / position or lesser index # is the maximum reach point from the previous indexes if (i > = maxReach): return - 1 # re-initialize the steps to the amount # of steps to reach maxReach from position i. step = maxReach - i; return - 1 # Driver program to test above function arr = [ 1 , 3 , 5 , 8 , 9 , 2 , 6 , 7 , 6 , 8 , 9 ] size = len (arr) # Calling the minJumps function print ( "Minimum number of jumps to reach end is % d " % minJumps(arr, size)) # This code is contributed by Aditi Sharma |
C#
// C# program to count Minimum // number of jumps to reach end using System; class GFG { static int minJumps( int [] arr) { if (arr.Length <= 1) return 0; // Return -1 if not // possible to jump if (arr[0] == 0) return -1; // initialization int maxReach = arr[0]; int step = arr[0]; int jump = 1; // Start traversing array for ( int i = 1; i < arr.Length; i++) { // Check if we have reached // the end of the array if (i == arr.Length - 1) return jump; // updating maxReach maxReach = Math.Max(maxReach, i + arr[i]); // we use a step to get // to the current index step--; // If no further steps left if (step == 0) { // we must have used a jump jump++; // Check if the current index/position // or lesser index is the maximum reach // point from the previous indexes if (i >= maxReach) return -1; // re-initialize the steps to // the amount of steps to reach // maxReach from position i. step = maxReach - i; } } return -1; } // Driver Code public static void Main() { int [] arr = new int [] { 1, 3, 5, 8, 9, 2, 6, 7, 6, 8, 9 }; // calling minJumps method Console.Write(minJumps(arr)); } } // This code is contributed // by nitin mittal |
PHP
<?php // PHP program to count Minimum number // of jumps to reach end // Returns minimum number of jumps to reach arr[n-1] from arr[0] function minJumps(& $arr , $n ) { // The number of jumps needed to reach the starting index is 0 if ( $n <= 1) return 0; // Return -1 if not possible to jump if ( $arr [0] == 0) return -1; // initialization // stores all time the maximal reachable index in the array. $maxReach = $arr [0]; // stores the number of steps we can still take $step = $arr [0]; //stores the number of jumps necessary to reach that // maximal reachable position. $jump =1; // Start traversing array $i =1; for ( $i = 1; $i < $n ; $i ++) { // Check if we have reached the end of the array if ( $i == $n -1) return $jump ; // updating maxReach $maxReach = max( $maxReach , $i + $arr [ $i ]); // we use a step to get to the current index $step --; // If no further steps left if ( $step == 0) { // we must have used a jump $jump ++; // Check if the current index/position or lesser index // is the maximum reach point from the previous indexes if ( $i >= $maxReach ) return -1; // re-initialize the steps to the amount // of steps to reach maxReach from position i. $step = $maxReach - $i ; } } return -1; } // Driver program to test above function $arr = array (1, 3, 5, 8, 9, 2, 6, 7, 6, 8, 9); $size = sizeof( $arr )/sizeof( $arr [0]); // Calling the minJumps function echo "Minimum number of jumps to reach end is " . minJumps( $arr , $size ); return 0; // This code is contributed by Ita_c. ?> |
Javascript
<script> // Javascript program to count Minimum number // Returns minimum number of jumps // to reach arr[n-1] from arr[0] function minJumps(arr,n) { // The number of jumps needed to // reach the starting index is 0 if (n <= 1) return 0; // Return -1 if not possible to jump if (arr[0] == 0) return -1; // initialization // stores all time the maximal // reachable index in the array. let maxReach = arr[0]; // stores the number of steps // we can still take let step = arr[0]; // stores the number of jumps // necessary to reach that maximal // reachable position. let jump = 1; // Start traversing array let i = 1; for (i = 1; i < n; i++) { // Check if we have reached the end of the array if (i == n - 1) return jump; // updating maxReach maxReach =Math.max(maxReach, i + arr[i]); // we use a step to get to the current index step--; // If no further steps left if (step == 0) { // we must have used a jump jump++; // Check if the current index/position or lesser index // is the maximum reach point from the previous indexes if (i >= maxReach) return -1; // re-initialize the steps to the amount // of steps to reach maxReach from position i. step = maxReach - i; } } return -1; } // Driver program to test above function var arr = [ 1, 3, 5, 8, 9, 2, 6, 7, 6, 8,9 ]; let size = arr.length; // Calling the minJumps function document.write( "Minimum number of jumps to reach end is " + minJumps(arr, size)); // This code is contributed by // Potta Lokesh </script> |
3
Complexity Analysis:
- Time complexity: O(n), Only one traversal of the array is needed.
- Auxiliary Space: O(1), There is no space required.
This article is contributed by Sahil Chhabra and Gaurav Miglani. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above.
Another Approach:
- For solving minimum jumps to reach the end of the array,
- For every jump index, we consider needing to evaluate the corresponding step values in the index and using the index value divides the array into sub-parts and find out the maximum steps covered index.
- The following code and explanation will give you a clear idea:
- In each sub-array find out the max distance covered index as the first part of the array, and the second array
Input Array : {1, 3, 5, 9, 6, 2, 6, 7, 6, 8, 9} -> index position starts with 0
Steps :
Initial step is considering the first index and incrementing the jump
Jump = 1
1, { 3, 5, 9, 6, 2, 6, 7, 6, 8, 9} -> 1 is considered as a first jump
next step
From the initial step, there is only one step to move so
Jump = 2
1,3, { 5, 9, 6,2, 6, 7, 6, 8, 9} -> 1 is considered as a first jump
next step
Now we have the flexibility to choose any of {5,9,6} because the last step says we can move up to 3 steps
Consider it as a subarray, evaluate the max distance covers with each index position
As {5,9,6} index positions are {2,3,4}
so the total farther steps we can cover:
{7,12,10} -> we can assume it as {7,12} & {10} are 2 sub-arrays where left part of arrays says max distance covered with 2 steps and right side array says max steps cover with remaining values
next step:
Considering the maximum distance covered in first array we iterate the remaining next elements
1,3,9 {6,2, 6, 7, 6, 8, 9}
From above step we already visited the 4th index we continue with next 5th index as explained above
{6,2, 6, 7, 6, 8, 9} index positions {4,5,6,7,8,9,10}
{10,7,12,14,14,17,19}
Max step covers here is 19 which corresponding index is 10
C++
// C++ program to illustrate Minimum // number of jumps to reach end #include <iostream> using namespace std; // Returns minimum number of jumps // to reach arr[n-1] from arr[0] int minJumps( int arr[], int n) { // The number of jumps needed to // reach the starting index is 0 if (n <= 1) return 0; // Return -1 if not possible to jump if (arr[0] == 0) return -1; // Stores the number of jumps // necessary to reach that maximal // reachable position. int jump = 1; // Stores the subarray last index int subArrEndIndex = arr[0]; int i = 1; // Maximum steps covers in // first half of sub array int subArrFistHalfMaxSteps = 0; // Maximum steps covers // in second half of sub array int subArrSecondHalfMaxSteps = 0; // Start traversing array for (i = 1; i < n;) { subArrEndIndex = i + subArrEndIndex; // Check if we have reached // the end of the array if (subArrEndIndex >= n) return jump; int firstHalfMaxStepIndex = 0; // Iterate the sub array // and find out the maxsteps // cover index for (; i < subArrEndIndex; i++) { int stepsCanCover = arr[i] + i; if (subArrFistHalfMaxSteps < stepsCanCover) { subArrFistHalfMaxSteps = stepsCanCover; subArrSecondHalfMaxSteps = 0; firstHalfMaxStepIndex = i; } else if (subArrSecondHalfMaxSteps < stepsCanCover) { subArrSecondHalfMaxSteps = stepsCanCover; } } if (i > subArrFistHalfMaxSteps) return -1; jump++; // Next subarray end index // and so far calculated sub // array max step cover value subArrEndIndex = arr[firstHalfMaxStepIndex]; subArrFistHalfMaxSteps = subArrSecondHalfMaxSteps; } return -1; } // Driver program to test above function int main() { int arr[] = { 1, 3, 5, 8, 9, 2, 6, 7, 6, 8, 9 }; int size = sizeof (arr) / sizeof ( int ); // Calling the minJumps function cout << ( "Minimum number of jumps to reach end is %d " , minJumps(arr, size)); return 0; } |
Java
// Java program to illustrate Minimum // number of jumps to reach end import java.io.*; class GFG { // Returns minimum number of jumps // to reach arr[n-1] from arr[0] static int minJumps( int arr[], int n) { // The number of jumps needed to // reach the starting index is 0 if (n <= 1 ) return 0 ; // Return -1 if not possible to jump if (arr[ 0 ] == 0 ) return - 1 ; // Stores the number of jumps // necessary to reach that maximal // reachable position. int jump = 1 ; // Stores the subarray last index int subArrEndIndex = arr[ 0 ]; int i = 1 ; // Maximum steps covers in // first half of sub array int subArrFistHalfMaxSteps = 0 ; // Maximum steps covers // in second half of sub array int subArrSecondHalfMaxSteps = 0 ; // Start traversing array for (i = 1 ; i < n;) { subArrEndIndex = i + subArrEndIndex; // Check if we have reached // the end of the array if (subArrEndIndex >= n) return jump; int firstHalfMaxStepIndex = 0 ; // Iterate the sub array // and find out the maxsteps // cover index for (; i < subArrEndIndex; i++) { int stepsCanCover = arr[i] + i; if (subArrFistHalfMaxSteps < stepsCanCover) { subArrFistHalfMaxSteps = stepsCanCover; subArrSecondHalfMaxSteps = 0 ; firstHalfMaxStepIndex = i; } else if (subArrSecondHalfMaxSteps < stepsCanCover) { subArrSecondHalfMaxSteps = stepsCanCover; } } if (i > subArrFistHalfMaxSteps) return - 1 ; jump++; // Next subarray end index // and so far calculated sub // array max step cover value subArrEndIndex = arr[firstHalfMaxStepIndex]; subArrFistHalfMaxSteps = subArrSecondHalfMaxSteps; } return - 1 ; } // Driver program to test above function public static void main(String[] args) { int arr[] = { 1 , 3 , 5 , 8 , 9 , 2 , 6 , 7 , 6 , 8 , 9 }; int size = arr.length; // Calling the minJumps function System.out.println( "Minimum number of jumps to reach end is " + minJumps(arr, size)); } } |
Python3
# Python program to illustrate Minimum # number of jumps to reach end # Returns minimum number of jumps # to reach arr[n-1] from arr[0] def minJumps(arr, n): # The number of jumps needed to # reach the starting index is 0 if (n < = 1 ): return 0 # Return -1 if not possible to jump if (arr[ 0 ] = = 0 ): return - 1 # Stores the number of jumps # necessary to reach that maximal # reachable position. jump = 1 # Stores the subarray last index subArrEndIndex = arr[ 0 ] i = 1 # Maximum steps covers in # first half of sub array subArrFistHalfMaxSteps = 0 # Maximum steps covers # in second half of sub array subArrSecondHalfMaxSteps = 0 # Start traversing array for i in range ( 1 , n): subArrEndIndex = i + subArrEndIndex # Check if we have reached # the end of the array if (subArrEndIndex > = n): return jump firstHalfMaxStepIndex = 0 # Iterate the sub array # and find out the maxsteps # cover index j = i for j in range (i, subArrEndIndex): stepsCanCover = arr[j] + j if (subArrFistHalfMaxSteps < stepsCanCover): subArrFistHalfMaxSteps = stepsCanCover subArrSecondHalfMaxSteps = 0 firstHalfMaxStepIndex = j elif (subArrSecondHalfMaxSteps < stepsCanCover): subArrSecondHalfMaxSteps = stepsCanCover i = j if (i > subArrFistHalfMaxSteps): return - 1 jump + = 1 # Next subarray end index # and so far calculated sub # array max step cover value subArrEndIndex = arr[firstHalfMaxStepIndex] subArrFistHalfMaxSteps = subArrSecondHalfMaxSteps return - 1 # Driver program to test above function if __name__ = = '__main__' : arr = [ 1 , 3 , 5 , 8 , 9 , 2 , 6 , 7 , 6 , 8 , 9 ] size = len (arr) # Calling the minJumps function print ( "Minimum number of jumps to reach end is " , minJumps(arr, size)) |
C#
// C# program to illustrate Minimum // number of jumps to reach end using System; public class GFG { // Returns minimum number of jumps // to reach arr[n-1] from arr[0] static int minJumps( int [] arr, int n) { // The number of jumps needed to // reach the starting index is 0 if (n <= 1) return 0; // Return -1 if not possible to jump if (arr[0] == 0) return -1; // Stores the number of jumps // necessary to reach that maximal // reachable position. int jump = 1; // Stores the subarray last index int subArrEndIndex = arr[0]; int i = 1; // Maximum steps covers in // first half of sub array int subArrFistHalfMaxSteps = 0; // Maximum steps covers // in second half of sub array int subArrSecondHalfMaxSteps = 0; // Start traversing array for (i = 1; i < n;) { subArrEndIndex = i + subArrEndIndex; // Check if we have reached // the end of the array if (subArrEndIndex >= n) return jump; int firstHalfMaxStepIndex = 0; // Iterate the sub array // and find out the maxsteps // cover index for (; i < subArrEndIndex; i++) { int stepsCanCover = arr[i] + i; if (subArrFistHalfMaxSteps < stepsCanCover) { subArrFistHalfMaxSteps = stepsCanCover; subArrSecondHalfMaxSteps = 0; firstHalfMaxStepIndex = i; } else if (subArrSecondHalfMaxSteps < stepsCanCover) { subArrSecondHalfMaxSteps = stepsCanCover; } } if (i > subArrFistHalfMaxSteps) return -1; jump++; // Next subarray end index // and so far calculated sub // array max step cover value subArrEndIndex = arr[firstHalfMaxStepIndex]; subArrFistHalfMaxSteps = subArrSecondHalfMaxSteps; } return -1; } // Driver code static public void Main() { int [] arr = { 1, 3, 5, 8, 9, 2, 6, 7, 6, 8, 9 }; int size = arr.Length; // Calling the minJumps function Console.WriteLine( "Minimum number of jumps to reach end is " + minJumps(arr, size)); } } |
Javascript
<script> // Javascript program to count Minimum number // Returns minimum number of jumps // to reach arr[n-1] from arr[0] function minJumps(arr,n) { // The number of jumps needed to // reach the starting index is 0 if (n <= 1) return 0; // Return -1 if not possible to jump if (arr[0] == 0) return -1; // Stores the number of jumps // necessary to reach that maximal // reachable position. let jump = 1; // Stores the subarray last index let subArrEndIndex = arr[0]; let i = 1; // Maximum steps covers in // first half of sub array let subArrFistHalfMaxSteps = 0; // Maximum steps covers // in second half of sub array let subArrSecondHalfMaxSteps = 0; // Start traversing array for (i = 1; i < n;) { subArrEndIndex = i + subArrEndIndex; // Check if we have reached // the end of the array if (subArrEndIndex >= n) return jump; let firstHalfMaxStepIndex = 0; // Iterate the sub array // and find out the maxsteps // cover index for (; i < subArrEndIndex; i++) { let stepsCanCover = arr[i] + i; if (subArrFistHalfMaxSteps < stepsCanCover) { subArrFistHalfMaxSteps = stepsCanCover; subArrSecondHalfMaxSteps = 0; firstHalfMaxStepIndex = i; } else if (subArrSecondHalfMaxSteps < stepsCanCover) { subArrSecondHalfMaxSteps = stepsCanCover; } } if (i > subArrFistHalfMaxSteps) return -1; jump++; // Next subarray end index // and so far calculated sub // array max step cover value subArrEndIndex = arr[firstHalfMaxStepIndex]; subArrFistHalfMaxSteps = subArrSecondHalfMaxSteps; } return -1; } // Driver program to test above function var arr =[1, 3, 5, 8, 9, 2, 6, 7, 6, 8, 9 ]; let size = arr.length; // Calling the minJumps function document.write ( "Minimum number of jumps to reach end is " + minJumps(arr, size)); </script> |
3
Time Complexity: O(n) Only one traversal of an array is required.
Auxiliary Space: O(1), since no extra space has been taken.
Please Login to comment...