Minimum Number of Jumps to Reach end of Array Solution

Hello coders, I am here with another solution for the problem of “Minimum Number of Jumps to Reach end of Array Solution“.

Problem Statement:

Given an array of N integers arr[] where each element represents the max length of the jump that can be made forward from that element.

Find the minimum number of jumps to reach the end of the array (starting from the first element). If an element is 0, then you cannot move through that element.

Input :

N = 6

arr = {1, 4, 3, 2, 6, 7}

Output: 2

Minimum Number of Jumps to Reach end of Array Java Solution

class Solution{
    int minJumps(int arr[], int n){
    int jumps = 0, farthest = 0, currEnd = 0;
    for(int i=0;i<n-1;i++)
        farthest = max(farthest, i + arr[i]);
        if(farthest >= n-1)
            return 1 + jumps;
        if(i == farthest)
            return -1;
        if(i == currEnd)
            currEnd = farthest;
    return jumps;   

Disclaimer: This problem(Minimum Number of Jumps) is originally created by GeeksforGeeksCodesagar only provides a solution for it. This solution is only for Educational and learning purposes.

Leave a Comment