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.
Prerequisite: Minimum number of jumps to reach end | Set-1 ( O(n2) solution)
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.
Minimum number of jumps to reach end using Greedy approach:
The idea is to use greedy approach to find the minimum jumps needed to reach the end of an array by iterating through the array while maintaining the maximum reachable index and the remaining steps in the current jump and updating these values based on the array elements.
Follow the steps below to solve the problem:
- Initialize the variables maxReach, step, and jump to keep track of the maximum reachable index, the remaining steps at the current position, and the number of jumps taken, respectively.
- Traverse the array and update the maximum reachable index based on the sum of the current index and its corresponding array value. This helps determine how far the current jump can take us.
- If the remaining steps for the current jump are exhausted, a new jump is initiated.
- If the current index surpasses the maximum reachable index, indicating no further progress, -1 is returned meaning no solution is found.
Below is the implementation of above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int max( int x, int y)
{
return (x > y) ? x : y;
}
int minJumps( int arr[], int n)
{
if (n <= 1)
return 0;
if (arr[0] >= n-1)
return 1;
if (arr[0] == 0)
return -1;
int maxReach = arr[0];
int step = arr[0];
int jump = 1;
int i = 1;
for (i = 1; i < n; i++) {
if (i == n - 1)
return jump;
if (arr[i] >= (n-1) - i)
return jump + 1;
maxReach = max(maxReach, i + arr[i]);
step--;
if (step == 0) {
jump++;
if (i >= maxReach)
return -1;
step = maxReach - i;
}
}
return -1;
}
int main()
{
int arr[] = { 1, 3, 5, 8, 9, 2, 6, 7, 6, 8, 9 };
int size = sizeof (arr) / sizeof ( int );
cout << ( "Minimum number of jumps to reach end is %d " ,
minJumps(arr, size));
return 0;
}
|
C
#include <stdio.h>
int max( int x, int y) { return (x > y) ? x : y; }
int minJumps( int arr[], int n)
{
if (n <= 1)
return 0;
if (arr[0] >= n-1)
return 1;
if (arr[0] == 0)
return -1;
int maxReach = arr[0];
int step = arr[0];
int jump = 1;
int i = 1;
for (i = 1; i < n; i++) {
if (i == n - 1)
return jump;
if (arr[i] >= (n-1) - i)
return jump + 1;
maxReach = max(maxReach, i + arr[i]);
step--;
if (step == 0) {
jump++;
if (i >= maxReach)
return -1;
step = maxReach - i;
}
}
return -1;
}
int main()
{
int arr[] = { 1, 3, 5, 8, 9, 2, 6, 7, 6, 8, 9 };
int size = sizeof (arr) / sizeof ( int );
printf (
"Minimum number of jumps to reach end is %d " ,
minJumps(arr, size));
return 0;
}
|
Java
class Test {
static int minJumps( int arr[])
{
if (arr.length <= 1 )
return 0 ;
if (arr[ 0 ] >= arr.length- 1 )
return 1 ;
if (arr[ 0 ] == 0 )
return - 1 ;
int maxReach = arr[ 0 ];
int step = arr[ 0 ];
int jump = 1 ;
for ( int i = 1 ; i < arr.length; i++) {
if (i == arr.length - 1 )
return jump;
if (arr[i] >= (arr.length- 1 ) - i)
return jump + 1 ;
maxReach = Math.max(maxReach, i + arr[i]);
step--;
if (step == 0 ) {
jump++;
if (i >= maxReach)
return - 1 ;
step = maxReach - i;
}
}
return - 1 ;
}
public static void main(String[] args)
{
int arr[] = new int [] { 1 , 3 , 5 , 8 , 9 , 2 , 6 , 7 , 6 , 8 , 9 };
System.out.println(minJumps(arr));
}
}
|
Python3
def minJumps(arr, n):
if (n < = 1 ):
return 0
if (arr[ 0 ] > = n - 1 ):
return 1
if (arr[ 0 ] = = 0 ):
return - 1
maxReach = arr[ 0 ]
step = arr[ 0 ]
jump = 1
for i in range ( 1 , n):
if (i = = n - 1 ):
return jump
if (arr[i] > = (n - 1 ) - i):
return jump + 1
maxReach = max (maxReach, i + arr[i])
step - = 1 ;
if (step = = 0 ):
jump + = 1
if (i > = maxReach):
return - 1
step = maxReach - i;
return - 1
arr = [ 1 , 3 , 5 , 8 , 9 , 2 , 6 , 7 , 6 , 8 , 9 ]
size = len (arr)
print ( "Minimum number of jumps to reach end is % d " % minJumps(arr, size))
|
C#
using System;
class GFG {
static int minJumps( int [] arr)
{
if (arr.Length <= 1)
return 0;
if (arr[0] == 0)
return -1;
int maxReach = arr[0];
int step = arr[0];
int jump = 1;
for ( int i = 1; i < arr.Length; i++) {
if (i == arr.Length - 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;
}
public static void Main()
{
int [] arr = new int [] { 1, 3, 5, 8, 9, 2,
6, 7, 6, 8, 9 };
Console.Write(minJumps(arr));
}
}
|
Javascript
<script>
function minJumps(arr,n)
{
if (n <= 1)
return 0;
if (arr[0] == 0)
return -1;
let maxReach = arr[0];
let step = arr[0];
let jump = 1;
let i = 1;
for (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;
}
var arr = [ 1, 3, 5, 8, 9, 2, 6, 7, 6, 8,9 ];
let size = arr.length;
document.write( "Minimum number of jumps to reach end is " +
minJumps(arr, size));
</script>
|
PHP
<?php
function minJumps(& $arr , $n )
{
if ( $n <= 1)
return 0;
if ( $arr [0] == 0)
return -1;
$maxReach = $arr [0];
$step = $arr [0];
$jump =1;
$i =1;
for ( $i = 1; $i < $n ; $i ++)
{
if ( $i == $n -1)
return $jump ;
$maxReach = max( $maxReach , $i + $arr [ $i ]);
$step --;
if ( $step == 0)
{
$jump ++;
if ( $i >= $maxReach )
return -1;
$step = $maxReach - $i ;
}
}
return -1;
}
$arr = array (1, 3, 5, 8, 9, 2, 6, 7, 6, 8, 9);
$size = sizeof( $arr )/sizeof( $arr [0]);
echo "Minimum number of jumps to reach end is "
. minJumps( $arr , $size );
return 0;
?>
|
Complexity Analysis:
- Time complexity: O(n), Only one traversal of the array is needed.
- Auxiliary Space: O(1), There is no space required.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
06 Sep, 2023
Like Article
Save Article