Open In App

Minimum number of jumps to reach end | Set 2 (O(n) solution)

Last Updated : 23 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Given an array arr[] of non-negative numbers. Each number tells you the maximum number of steps you can jump forward from that position.
For example:

  • If arr[i] = 3, you can jump to index i + 1, i + 2, or i + 3 from position i.
  • If arr[i] = 0, you cannot jump forward from that position.

Your task is to find the minimum number of jumps needed to move from the first position in the array to the last position.

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

Examples: 

Input: 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. From here we jump to 5th element with value 9, and from here we will jump to the last.

Input: 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.

Input: arr = [0, 10, 20]
Output: -1
Explanation: We cannot go anywhere from the 1st element.

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. We iterate through the array and maintain two values: the maximum reachable index and the current reachable index and update them based on the array elements.

Follow the steps below to solve the problem:

  • Initialize the variables maxReach = 0, currReach = 0, and jump = 0 to keep track of the maximum reachable index, the current reachable index at the ith position, and the number of jumps taken to reach the current reachable index, 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 current index is equal to the current reachable index, then a jump is required. We choose our jump in such a way that it takes us to the maximum possible index. Increment jump by 1 and update currReach to maxReach.
  • If the current index is equal to the maximum reachable index, it indicates that we cannot move beyond this point, so return -1.


C++
// C++ program to count Minimum number
// of jumps to reach end
#include <iostream>
#include <vector>
using namespace std;

int minJumps(vector<int>& arr) {
  	int n = arr.size();

    // Return -1 if not possible to jump
    if (arr[0] == 0)
        return -1;

    // Stores the maximal reachable index
    int maxReach = 0;

    // stores the number of steps  we can still take
    int currReach = 0;

    // stores the number of jumps needed 
  	// to reach current reachable index
    int jump = 0;

    // Start traversing array
    for (int i = 0; i < n; i++) {
      	maxReach = max(maxReach, i+arr[i]);
      
      	// If we can reach last index by jumping 
      	// from current position return Jump + 1
      	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() {
    vector<int> arr = {1, 3, 5, 8, 9, 2, 6, 7, 6, 8, 9};
    
    cout << minJumps(arr);
    return 0;
}
C
// C program to count Minimum number
// of jumps to reach end
#include <stdio.h>

// Returns minimum number of jumps
// to reach arr[n-1] from arr[0]
int minJumps(int arr[], int n) {

    // Return -1 if not possible to jump
    if (arr[0] == 0)
        return -1;

    // Stores the maximal reachable index
    int maxReach = 0;

    // stores the number of steps we can still take
    int currReach = 0;

    // stores the number of jumps needed 
    // to reach current reachable index
    int jump = 0;

    // Start traversing array
    for (int i = 0; i < n; i++) {
        maxReach = (maxReach > i + arr[i]) ? maxReach : (i + arr[i]);

        // If we can reach last index by jumping 
        // from current position return Jump + 1
        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, 5, 8, 9, 2, 6, 7, 6, 8, 9};
    int n = sizeof(arr) / sizeof(arr[0]);

    printf("%d", minJumps(arr, n));
    return 0;
}
Java
// Java program to count Minimum number
// of jumps to reach end
class GfG {

    // Returns minimum number of jumps
    // to reach arr[n-1] from arr[0]
    static int minJumps(int[] arr) {
        int n = arr.length;

        // Return -1 if not possible to jump
        if (arr[0] == 0)
            return -1;

        // Stores the maximal reachable index
        int maxReach = 0;

        // stores the number of steps we can still take
        int currReach = 0;

        // stores the number of jumps needed 
        // to reach current reachable index
        int jump = 0;

        // Start traversing array
        for (int i = 0; i < n; i++) {
            maxReach = Math.max(maxReach, i + arr[i]);

            // If we can reach last index by jumping 
            // from current position return Jump + 1
            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, 5, 8, 9, 2, 6, 7, 6, 8, 9};
        System.out.println(minJumps(arr));
    }
}
Python
# 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 = len(arr)

    # Return -1 if not possible to jump
    if arr[0] == 0:
        return -1

    # Stores the maximal reachable index
    maxReach = 0

    # stores the number of steps we can still take
    currReach = 0

    # stores the number of jumps needed 
    # to reach current reachable index
    jump = 0

    # Start traversing array
    for i in range(n):
        maxReach = max(maxReach, i + arr[i])

        # If we can reach last index by jumping 
        # from current position return Jump + 1
        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, 5, 8, 9, 2, 6, 7, 6, 8, 9]
    print(minJumps(arr))
C#
// C# program to count Minimum number
// of jumps to reach end
using System;

class GfG {

    // Returns minimum number of jumps
    // to reach arr[n-1] from arr[0]
    static int minJumps(int[] arr) {
        int n = arr.Length;

        // Return -1 if not possible to jump
        if (arr[0] == 0)
            return -1;

        // Stores the maximal reachable index
        int maxReach = 0;

        // stores the number of steps we can still take
        int currReach = 0;

        // stores the number of jumps needed 
        // to reach current reachable index
        int jump = 0;

        // Start traversing array
        for (int i = 0; i < n; i++) {
            maxReach = Math.Max(maxReach, i + arr[i]);

            // If we can reach last index by jumping 
            // from current position return Jump + 1
            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, 5, 8, 9, 2, 6, 7, 6, 8, 9};
        Console.WriteLine(minJumps(arr));
    }
}
JavaScript
// JavaScript 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) {
    let n = arr.length;

    // Return -1 if not possible to jump
    if (arr[0] === 0)
        return -1;

    // Stores the maximal reachable index
    let maxReach = 0;

    // stores the number of steps we can still take
    let currReach = 0;

    // stores the number of jumps needed 
    // to reach current reachable index
    let jump = 0;

    // Start traversing array
    for (let i = 0; i < n; i++) {
        maxReach = Math.max(maxReach, i + arr[i]);

        // If we can reach last index by jumping 
        // from current position return Jump + 1
        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, 5, 8, 9, 2, 6, 7, 6, 8, 9];
console.log(minJumps(arr));

Output
3

Time complexity: O(n), Only one traversal of the array is needed.
Auxiliary Space: O(1), There is no extra space required.

Related Article: Minimum number of jumps to reach end | Set-1 ( O(n2) solution)



Similar Reads