Open In App

Jump Game - Minimum Jumps to Reach End

Last Updated : 17 Mar, 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.

[Naive Approach] - Using Recursion - O(n ^ n) Time and O(n) Space

The idea is to recursively generate all possible way of reaching the end of the array and find the one with the minimum steps required. To do so, start from the first element and for each element arr[i] recursively call for all the elements reachable from that element (i.e. i + arr[i]) and return the one with the minimum steps required.

Start from the first element and recursively call for all the elements reachable from the first element. The minimum number of jumps to reach end from first can be calculated using the minimum value from the recursive calls. 

minJumps(start, end) = 1 + Min(minJumps(k, end)) for all k reachable from start.

C++
// c++ program to find the minimum number
// of jumps to reach the end of the array
#include <bits/stdc++.h>
using namespace std;

int minJumpsRecur(int i, vector<int> &arr) {
    
    // Return 0 when last element is reached.
    if (i >= arr.size()-1)
        return 0;

    // Traverse through all the points
    // reachable from arr[i].
    // Recursively, get the minimum number
    // of jumps needed to reach array end from
    // these points.
    int ans = INT_MAX;
    for (int j=i+1; j<=i+arr[i]; j++) {
        int val = minJumpsRecur(j, arr);
        if (val != INT_MAX)
            ans = min(ans, 1+val);
    }

    return ans;
}

// Function to return the minimum number
// of jumps to reach arr[h] from arr[l]
int minJumps(vector<int>& arr) {

    int ans =  minJumpsRecur(0, arr);
    
    // If end cannot be reached.
    if (ans == INT_MAX) 
        return -1;
        
    return ans;
}

int main() {
    vector<int> arr = { 1, 3, 5, 8, 9, 2, 6, 7, 6, 8, 9 };
    cout << minJumps(arr);
    return 0;
}
Java
// Java program to find the minimum number
// of jumps to reach the end of the array

import java.util.Arrays;

class GfG {

    static int minJumpsRecur(int i, int[] arr) {
        
        // Return 0 when last element is reached.
        if (i >= arr.length - 1)
            return 0;

        // Traverse through all the points
        // reachable from arr[i].
        // Recursively, get the minimum number
        // of jumps needed to reach array end from
        // these points.
        int ans = Integer.MAX_VALUE;
        for (int j = i + 1; j <= i + arr[i]; j++) {
            int val = minJumpsRecur(j, arr);
            if (val != Integer.MAX_VALUE)
                ans = Math.min(ans, 1 + val);
        }

        return ans;
    }

    static int minJumps(int[] arr) {

        int ans = minJumpsRecur(0, arr);
        
        // If end cannot be reached.
        if (ans == Integer.MAX_VALUE) 
            return -1;
            
        return ans;
    }

    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 find the minimum number
# of jumps to reach the end of the array

import sys

def minJumpsRecur(i, arr):
    
    # Return 0 when last element is reached.
    if i >= len(arr) - 1:
        return 0

    # Traverse through all the points
    # reachable from arr[i].
    # Recursively, get the minimum number
    # of jumps needed to reach array end from
    # these points.
    ans = sys.maxsize
    for j in range(i + 1, i + arr[i] + 1):
        val = minJumpsRecur(j, arr)
        if val != sys.maxsize:
            ans = min(ans, 1 + val)

    return ans

def minJumps(arr):
    ans = minJumpsRecur(0, arr)
    
    # If end cannot be reached.
    if ans == sys.maxsize:
        return -1
        
    return ans

if __name__ == "__main__":
    arr = [1, 3, 5, 8, 9, 2, 6, 7, 6, 8, 9]
    print(minJumps(arr))
C#
// C# program to find the minimum number
// of jumps to reach the end of the array

using System;

class GfG {

    static int minJumpsRecur(int i, int[] arr) {
        
        // Return 0 when last element is reached.
        if (i >= arr.Length - 1)
            return 0;

        // Traverse through all the points
        // reachable from arr[i].
        // Recursively, get the minimum number
        // of jumps needed to reach array end from
        // these points.
        int ans = int.MaxValue;
        for (int j = i + 1; j <= i + arr[i]; j++) {
            int val = minJumpsRecur(j, arr);
            if (val != int.MaxValue)
                ans = Math.Min(ans, 1 + val);
        }

        return ans;
    }

    static int minJumps(int[] arr) {

        int ans = minJumpsRecur(0, arr);
        
        // If end cannot be reached.
        if (ans == int.MaxValue) 
            return -1;
            
        return ans;
    }

    static void Main() {
        int[] arr = { 1, 3, 5, 8, 9, 2, 6, 7, 6, 8, 9 };
        Console.WriteLine(minJumps(arr));
    }
}
JavaScript
// JavaScript program to find the minimum number
// of jumps to reach the end of the array

function minJumpsRecur(i, arr) {
    
    // Return 0 when last element is reached.
    if (i >= arr.length - 1)
        return 0;

    // Traverse through all the points
    // reachable from arr[i].
    // Recursively, get the minimum number
    // of jumps needed to reach array end from
    // these points.
    let ans = Infinity;
    for (let j = i + 1; j <= i + arr[i]; j++) {
        let val = minJumpsRecur(j, arr);
        if (val != Infinity)
            ans = Math.min(ans, 1 + val);
    }

    return ans;
}

function minJumps(arr) {
    let ans = minJumpsRecur(0, arr);
    
    // If end cannot be reached.
    if (ans == Infinity) 
        return -1;
        
    return ans;
}

let arr = [1, 3, 5, 8, 9, 2, 6, 7, 6, 8, 9];
console.log(minJumps(arr));

Output
3

[Better Approach 1] - Using Top-Down DP (Memoization) - O(n^2) Time and O(n) Space

  • The above approach can be optimized using memoization to avoid computing the overlapping subproblems multiple times. For example in array, arr[] = {1, 3, 5, 8, 9, 2, 6, 7, 6, 8, 9} minJumps(3, 9) will be called two times as arr[3] is reachable from arr[1] and arr[2].
  • So this problem has both properties (optimal substructure and overlapping subproblems) of Dynamic Programming.
  • To do so, create an array memo[] of size n, where each element memo[i] stores the minimum steps required to reach end of array from index i.
  • For each recursive call, check if subproblem is already calculated, if so return the stored value, else operate similar to above approach.
C++
// c++ program to find the minimum number
// of jumps to reach the end of the array
#include <bits/stdc++.h>
using namespace std;

int minJumpsRecur(int i, vector<int> &arr, vector<int> &memo) {
    
    // Return 0 when last element is reached.
    if (i == arr.size()-1)
        return 0;
        
    // If value for current index is memoized,
    // then return it.
    if (memo[i] != -1) return memo[i];

    // Traverse through all the points
    // reachable from arr[i].
    // Recursively, get the minimum number
    // of jumps needed to reach array end from
    // these points.
    int ans = INT_MAX;
    for (int j=i+1; j<=i+arr[i] && j<arr.size(); j++) {
        int val = minJumpsRecur(j, arr, memo);
        if (val != INT_MAX)
            ans = min(ans, 1+val);
    }
    
    // Memoize the value and 
    // return it.
    return memo[i] = ans;
}

// Function to return the minimum number
// of jumps to reach arr[h] from arr[l]
int minJumps(vector<int>& arr) {
    
    // array to memoize values
    vector<int> memo(arr.size(), -1);
    
    int ans =  minJumpsRecur(0, arr, memo);
    
    // If end cannot be reached.
    if (ans == INT_MAX) 
        return -1;
        
    return ans;
}

int main() {
    vector<int> arr = { 1, 3, 5, 8, 9, 2, 6, 7, 6, 8, 9 };
    cout << minJumps(arr);
    return 0;
}
Java
// Java program to find the minimum number
// of jumps to reach the end of the array

class GfG {

    static int minJumpsRecur(int i, int[] arr, int[] memo) {

        // Return 0 when last element is reached.
        if (i == arr.length - 1)
            return 0;

        // If value for current index is memoized,
        // then return it.
        if (memo[i] != -1) return memo[i];

        // Traverse through all the points
        // reachable from arr[i].
        // Recursively, get the minimum number
        // of jumps needed to reach array end from
        // these points.
        int ans = Integer.MAX_VALUE;
        for (int j = i + 1; j <= i + arr[i] && j < arr.length; j++) {
            int val = minJumpsRecur(j, arr, memo);
            if (val != Integer.MAX_VALUE)
                ans = Math.min(ans, 1 + val);
        }

        // Memoize the value and 
        // return it.
        return memo[i] = ans;
    }

    // Function to return the minimum number
    // of jumps to reach arr[h] from arr[l]
    static int minJumps(int[] arr) {

        // array to memoize values
        int[] memo = new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
            memo[i] = -1;
        }

        int ans = minJumpsRecur(0, arr, memo);

        // If end cannot be reached.
        if (ans == Integer.MAX_VALUE)
            return -1;

        return ans;
    }

    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 find the minimum number
# of jumps to reach the end of the array

def minJumpsRecur(i, arr, memo):

    # Return 0 when last element is reached.
    if i == len(arr) - 1:
        return 0

    # If value for current index is memoized,
    # then return it.
    if memo[i] != -1:
        return memo[i]

    # Traverse through all the points
    # reachable from arr[i].
    # Recursively, get the minimum number
    # of jumps needed to reach array end from
    # these points.
    ans = float('inf')
    for j in range(i + 1, min(i + arr[i] + 1, len(arr))):
        val = minJumpsRecur(j, arr, memo)
        if val != float('inf'):
            ans = min(ans, 1 + val)

    # Memoize the value and 
    # return it.
    memo[i] = ans
    return ans

# Function to return the minimum number
# of jumps to reach arr[h] from arr[l]
def minJumps(arr):

    # array to memoize values
    memo = [-1] * len(arr)

    ans = minJumpsRecur(0, arr, memo)

    # If end cannot be reached.
    if ans == float('inf'):
        return -1

    return ans

if __name__ == "__main__":
    arr = [1, 3, 5, 8, 9, 2, 6, 7, 6, 8, 9]
    print(minJumps(arr))
C#
// C# program to find the minimum number
// of jumps to reach the end of the array

using System;

class GfG {

    static int minJumpsRecur(int i, int[] arr, int[] memo) {

        // Return 0 when last element is reached.
        if (i == arr.Length - 1)
            return 0;

        // If value for current index is memoized,
        // then return it.
        if (memo[i] != -1) return memo[i];

        // Traverse through all the points
        // reachable from arr[i].
        // Recursively, get the minimum number
        // of jumps needed to reach array end from
        // these points.
        int ans = int.MaxValue;
        for (int j = i + 1; j <= i + arr[i] && j < arr.Length; j++) {
            int val = minJumpsRecur(j, arr, memo);
            if (val != int.MaxValue)
                ans = Math.Min(ans, 1 + val);
        }

        // Memoize the value and 
        // return it.
        return memo[i] = ans;
    }

    // Function to return the minimum number
    // of jumps to reach arr[h] from arr[l]
    static int minJumps(int[] arr) {

        // array to memoize values
        int[] memo = new int[arr.Length];
        for (int i = 0; i < arr.Length; i++) {
            memo[i] = -1;
        }

        int ans = minJumpsRecur(0, arr, memo);

        // If end cannot be reached.
        if (ans == int.MaxValue)
            return -1;

        return ans;
    }

    static void Main(string[] args) {
        int[] arr = {1, 3, 5, 8, 9, 2, 6, 7, 6, 8, 9};
        Console.WriteLine(minJumps(arr));
    }
}
JavaScript
// JavaScript program to find the minimum number
// of jumps to reach the end of the array

function minJumpsRecur(i, arr, memo) {

    // Return 0 when last element is reached.
    if (i === arr.length - 1)
        return 0;

    // If value for current index is memoized,
    // then return it.
    if (memo[i] !== -1) return memo[i];

    // Traverse through all the points
    // reachable from arr[i].
    // Recursively, get the minimum number
    // of jumps needed to reach array end from
    // these points.
    let ans = Infinity;
    for (let j = i + 1; j <= i + arr[i] && j < arr.length; j++) {
        let val = minJumpsRecur(j, arr, memo);
        if (val !== Infinity)
            ans = Math.min(ans, 1 + val);
    }

    // Memoize the value and 
    // return it.
    memo[i] = ans;
    return ans;
}

// Function to return the minimum number
// of jumps to reach arr[h] from arr[l]
function minJumps(arr) {

    // array to memoize values
    let memo = new Array(arr.length).fill(-1);

    let ans = minJumpsRecur(0, arr, memo);

    // If end cannot be reached.
    if (ans === Infinity)
        return -1;

    return ans;
}

let arr = [1, 3, 5, 8, 9, 2, 6, 7, 6, 8, 9];
console.log(minJumps(arr));

Output
3

[Better Approach 2] - Using Bottom-Up DP (Tabulation) - O(n^2) Time and O(n) Space

The above approach can further be optimized using bottom-up dp (tabulation) to minimize the space required for recursive stack. To do so, create an array dp[] of size n, where each element dp[i] stores the minimum steps required to reach end of array from index i. Start from the last index i.e. n-1, and for each index i compute the minimum steps for subarray i to n-1 and store the result in dp[i].

C++
// c++ program to find the minimum number
// of jumps to reach the end of the array
#include <bits/stdc++.h>
using namespace std;

// Function to return the minimum number
// of jumps to reach arr end.
int minJumps(vector<int> &arr) {
    int n = arr.size();

    // array to memoize values
    vector<int> dp(n, INT_MAX);
    dp[n - 1] = 0;

    for (int i = n - 2; i >= 0; i--) {
        for (int j = i + 1; j <= i + arr[i] && j < n; j++) {
            if (dp[j] != INT_MAX) {
                dp[i] = min(dp[i], 1 + dp[j]);
            }
        }
    }

    // If end cannot be reached.
    if (dp[0] == INT_MAX)
        return -1;

    return dp[0];
}

int main() {
  
    vector<int> arr = {1, 3, 5, 8, 9, 2, 6, 7, 6, 8, 9};
    cout << minJumps(arr);
    return 0;
}
Java
// Java program to find the minimum number
// of jumps to reach the end of the array
import java.util.Arrays;

class GfG {

    // Function to return the minimum number
    // of jumps to reach arr end.
    static int minJumps(int[] arr) {
        int n = arr.length;

        // array to memoize values
        int[] dp = new int[n];
        Arrays.fill(dp, Integer.MAX_VALUE);
        dp[n - 1] = 0;

        for (int i = n - 2; i >= 0; i--) {
            for (int j = i + 1; j <= i + arr[i] && j < n; j++) {
                if (dp[j] != Integer.MAX_VALUE) {
                    dp[i] = Math.min(dp[i], 1 + dp[j]);
                }
            }
        }

        // If end cannot be reached.
        if (dp[0] == Integer.MAX_VALUE)
            return -1;

        return dp[0];
    }

    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 find the minimum number
# of jumps to reach the end of the array

def minJumps(arr):
    n = len(arr)

    # array to memoize values
    dp = [float('inf')] * n
    dp[n - 1] = 0

    for i in range(n - 2, -1, -1):
        for j in range(i + 1, min(i + arr[i] + 1, n)):
            if dp[j] != float('inf'):
                dp[i] = min(dp[i], 1 + dp[j])

    # If end cannot be reached.
    if dp[0] == float('inf'):
        return -1

    return dp[0]

if __name__ == "__main__":
    arr = [1, 3, 5, 8, 9, 2, 6, 7, 6, 8, 9]
    print(minJumps(arr))
C#
// C# program to find the minimum number
// of jumps to reach the end of the array

using System;

class GfG {

    // Function to return the minimum number
    // of jumps to reach arr end.
    static int minJumps(int[] arr) {
        int n = arr.Length;

        // array to memoize values
        int[] dp = new int[n];
        Array.Fill(dp, int.MaxValue);
        dp[n - 1] = 0;

        for (int i = n - 2; i >= 0; i--) {
            for (int j = i + 1; j <= i + arr[i] && j < n; j++) {
                if (dp[j] != int.MaxValue) {
                    dp[i] = Math.Min(dp[i], 1 + dp[j]);
                }
            }
        }

        // If end cannot be reached.
        if (dp[0] == int.MaxValue)
            return -1;

        return dp[0];
    }

    static void Main(string[] args) {
        int[] arr = {1, 3, 5, 8, 9, 2, 6, 7, 6, 8, 9};
        Console.WriteLine(minJumps(arr));
    }
}
JavaScript
// JavaScript program to find the minimum number
// of jumps to reach the end of the array

function minJumps(arr) {
    let n = arr.length;

    // array to memoize values
    let dp = new Array(n).fill(Infinity);
    dp[n - 1] = 0;

    for (let i = n - 2; i >= 0; i--) {
        for (let j = i + 1; j <= i + arr[i] && j < n; j++) {
            if (dp[j] !== Infinity) {
                dp[i] = Math.min(dp[i], 1 + dp[j]);
            }
        }
    }

    // If end cannot be reached.
    if (dp[0] === Infinity)
        return -1;

    return dp[0];
}

let arr = [1, 3, 5, 8, 9, 2, 6, 7, 6, 8, 9];
console.log(minJumps(arr));

Output
3

[Expected Approach] - Using Greedy Approach - O(n) Time and O(1) Space

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.

This approach has been discussed in article Minimum number of jumps to reach end | Set 2 (O(n) solution).


Next Article

Similar Reads