Given an array arr[] where each element represents the maximum number of steps you can move forward from that position. Find the minimum number of jumps required to reach the last index, if you are starting from the 0th index.
Note: If it is not possible to reach the last index, return -1.
Examples:
Input: arr[] = [1, 3, 2, 2, 1, 4, 6]
Output: 4
Explanation: Starting at index 0, we jump to index 1. From there, we move to index 4, then to index 5, and from index 5 we jump to index 6. So the minimum jumps needed are 4.Input: arr[] = [0, 10, 20]
Output: -1
Explanation: we cannot jump forward from the beginning because arr[0]=0.
[Approach] By Greedy Jumping Strategy - O(n) Time and O(1) Space
The idea is to greedily keep track of the farthest index you can reach say(
maxReach) as you move along. You donât jump at every step â instead, you move within the current jumpâs range and only when that range ends, you take a jump and extend it tomaxReach. This way we can find the minimum number of jumps, since every jump is stretched to its farthest possible reach.
To implement this, we just maintain a few variables while iterating:
jumpsâ number of jumps taken so farcurrentEndâ end of the current jump rangemaxReachâ farthest index reachable so far
At each index, update maxReach = max(maxReach, i + arr[i]).
If you reach currentEnd, increment jumps and extend currentEnd = maxReach.
If at any point i == currentEnd but maxReach == currentEnd, it means we cannot reach to the end, return -1.
#include <iostream>
#include <vector>
using namespace std;
int minJumps(vector<int> &arr){
int n = arr.size();
if (arr[0] == 0)
return -1;
int maxReach = 0;
int currReach = 0;
int jump = 0;
for (int i = 0; i < n; i++){
maxReach = max(maxReach, i + arr[i]);
if (maxReach >= n - 1){
return jump + 1;
}
// Increment the Jump as we reached the
// Current Reachable index
if (i == currReach){
// If Max reach is same as current index
// then we can not jump further
if (maxReach == i){
return -1;
}
// If maxReach > current index then increment
// jump and update current reachable index
else{
jump++;
currReach = maxReach;
}
}
}
return -1;
}
int main()
{
vector<int> arr = {1, 3, 2, 2, 1, 4, 6};
cout << minJumps(arr);
return 0;
}
#include <stdio.h>
int minJumps(int arr[], int n)
{
if (arr[0] == 0)
return -1;
int maxReach = 0;
int currReach = 0;
int jump = 0;
for (int i = 0; i < n; i++){
maxReach = (maxReach > i + arr[i]) ? maxReach : (i + arr[i]);
if (maxReach >= n - 1){
return jump + 1;
}
// Increment the Jump as we reached the
// Current Reachable index
if (i == currReach){
// If Max reach is same as current index
// then we can not jump further
if (i == maxReach){
return -1;
}
// If Max reach > current index then increment
// jump and update current reachable index
else{
jump++;
currReach = maxReach;
}
}
}
return -1;
}
int main()
{
int arr[] = {1, 3, 2, 2, 1, 4, 6};
int n = sizeof(arr) / sizeof(arr[0]);
printf("%d", minJumps(arr, n));
return 0;
}
class GfG {
static int minJumps(int[] arr){
int n = arr.length;
if (arr[0] == 0)
return -1;
int maxReach = 0;
int currReach = 0;
int jump = 0;
for (int i = 0; i < n; i++) {
maxReach = Math.max(maxReach, i + arr[i]);
if (maxReach >= n - 1) {
return jump + 1;
}
// Increment the Jump as we reached the
// Current Reachable index
if (i == currReach) {
// If Max reach is same as current index
// then we can not jump further
if (i == maxReach) {
return -1;
}
// If Max reach > current index then
// increment jump and update current
// reachable index
else {
jump++;
currReach = maxReach;
}
}
}
return -1;
}
public static void main(String[] args)
{
int[] arr = { 1, 3, 2, 2, 1, 4, 6 };
System.out.println(minJumps(arr));
}
}
def minJumps(arr):
n = len(arr)
if arr[0] == 0:
return -1
maxReach = 0
currReach = 0
jump = 0
for i in range(n):
maxReach = max(maxReach, i + arr[i])
if maxReach >= n - 1:
return jump + 1
# Increment the Jump as we reached the
# Current Reachable index
if i == currReach:
# If Max reach is same as current index
# then we can not jump further
if i == maxReach:
return -1
# If Max reach > current index then increment
# jump and update current reachable index
else:
jump += 1
currReach = maxReach
return -1
if __name__ == "__main__":
arr = [1, 3, 2, 2, 1, 4, 6]
print(minJumps(arr))
using System;
class GfG {
static int minJumps(int[] arr)
{
int n = arr.Length;
if (arr[0] == 0)
return -1;
int maxReach = 0;
int currReach = 0;
int jump = 0;
for (int i = 0; i < n; i++) {
maxReach = Math.Max(maxReach, i + arr[i]);
if (maxReach >= n - 1) {
return jump + 1;
}
// Increment the Jump as we reached the
// Current Reachable index
if (i == currReach) {
// If Max reach is same as current index
// then we can not jump further
if (i == maxReach) {
return -1;
}
// If Max reach > current index then
// increment jump and update current
// reachable index
else {
jump++;
currReach = maxReach;
}
}
}
return -1;
}
static void Main()
{
int[] arr = { 1, 3, 2, 2, 1, 4, 6 };
Console.WriteLine(minJumps(arr));
}
}
function minJumps(arr)
{
let n = arr.length;
if (arr[0] === 0)
return -1;
let maxReach = 0;
let currReach = 0;
let jump = 0;
for (let i = 0; i < n; i++) {
maxReach = Math.max(maxReach, i + arr[i]);
if (maxReach >= n - 1) {
return jump + 1;
}
// Increment the Jump as we reached the
// Current Reachable index
if (i === currReach) {
// If Max reach is same as current index
// then we can not jump further
if (i === maxReach) {
return -1;
}
// If Max reach > current index then increment
// jump and update current reachable index
else {
jump++;
currReach = maxReach;
}
}
}
return -1;
}
// Driver Code
const arr = [ 1, 3, 2, 2, 1, 4, 6 ];
console.log(minJumps(arr));
Output
4
Related Article:Minimum number of jumps to reach end | Set-1 ( O(n2) solution)

