Arrays January 18 ,2026

Minimum Number of Jumps to Reach End

Problem Statement

You are given an integer array arr[] of size n.

Each element arr[i] represents the maximum number of steps you can jump forward from index i.

Return the minimum number of jumps required to reach the last index starting from index 0.

If the end is not reachable, return -1.

Example 1

Input

arr = [2,3,1,1,4]

Output

2

Explanation

Jump from index 0 → 1 → 4

Example 2

Input

arr = [1,1,0,1]

Output

-1

Why This Problem Is Important

Classic greedy interview problem
Tests optimization from brute force to greedy
Used in path & reachability problems
Foundation for interval coverage logic
Very frequently asked in product interviews

Approaches Overview

ApproachTechniqueTimeSpace
Approach 1Brute Force RecursionO(2ⁿ)O(n)
Approach 2Dynamic ProgrammingO(n²)O(n)
Approach 3Greedy (Optimal)O(n)O(1)

Approach 1: Brute Force Recursion

Idea

From each index, try all possible jumps and find the minimum jumps needed to reach the end.

Algorithm

  1. Start from index 0
  2. Try jumps from 1 to arr[i]
  3. Recursively compute minimum jumps
  4. Return minimum among all paths

Time & Space Complexity

Time: O(2ⁿ)
Space: O(n) (recursion stack)

C Implementation

#include 
#include 

int minJumps(int arr[], int n, int idx) {
    if (idx >= n - 1)
        return 0;

    if (arr[idx] == 0)
        return INT_MAX;

    int min = INT_MAX;

    for (int i = 1; i <= arr[idx]; i++) {
        int jumps = minJumps(arr, n, idx + i);
        if (jumps != INT_MAX && jumps + 1 < min)
            min = jumps + 1;
    }
    return min;
}

int main() {
    int arr[] = {2,3,1,1,4};
    int n = 5;

    int res = minJumps(arr, n, 0);
    printf("%d", res == INT_MAX ? -1 : res);
    return 0;
}

Output:

2

C++ Implementation

#include 
#include 
using namespace std;

int minJumps(int arr[], int n, int idx) {
    if (idx >= n - 1) return 0;
    if (arr[idx] == 0) return INT_MAX;

    int ans = INT_MAX;
    for (int i = 1; i <= arr[idx]; i++) {
        int jumps = minJumps(arr, n, idx + i);
        if (jumps != INT_MAX)
            ans = min(ans, jumps + 1);
    }
    return ans;
}

int main() {
    int arr[] = {2,3,1,1,4};
    int n = 5;
    int res = minJumps(arr, n, 0);
    cout << (res == INT_MAX ? -1 : res);
}

Output:

2

Java Implementation

class Main {
    static int minJumps(int[] arr, int idx) {
        if (idx >= arr.length - 1) return 0;
        if (arr[idx] == 0) return Integer.MAX_VALUE;

        int ans = Integer.MAX_VALUE;
        for (int i = 1; i <= arr[idx]; i++) {
            int jumps = minJumps(arr, idx + i);
            if (jumps != Integer.MAX_VALUE)
                ans = Math.min(ans, jumps + 1);
        }
        return ans;
    }

    public static void main(String[] args) {
        int[] arr = {2,3,1,1,4};
        int res = minJumps(arr, 0);
        System.out.print(res == Integer.MAX_VALUE ? -1 : res);
    }
}

Output:

2

Python Implementation

import sys

def minJumps(arr, idx):
    if idx >= len(arr) - 1:
        return 0
    if arr[idx] == 0:
        return sys.maxsize

    ans = sys.maxsize
    for i in range(1, arr[idx] + 1):
        jumps = minJumps(arr, idx + i)
        if jumps != sys.maxsize:
            ans = min(ans, jumps + 1)
    return ans

arr = [2,3,1,1,4]
res = minJumps(arr, 0)
print(-1 if res == sys.maxsize else res)

Output:

2

C# Implementation

using System;

class Program {
    static int MinJumps(int[] arr, int idx) {
        if (idx >= arr.Length - 1) return 0;
        if (arr[idx] == 0) return int.MaxValue;

        int ans = int.MaxValue;
        for (int i = 1; i <= arr[idx]; i++) {
            int jumps = MinJumps(arr, idx + i);
            if (jumps != int.MaxValue)
                ans = Math.Min(ans, jumps + 1);
        }
        return ans;
    }

    static void Main() {
        int[] arr = {2,3,1,1,4};
        int res = MinJumps(arr, 0);
        Console.Write(res == int.MaxValue ? -1 : res);
    }
}

Output:

2

 

Approach 2: Dynamic Programming

Idea

Let dp[i] store the minimum jumps needed to reach index i.

Algorithm

  1. Initialize dp[0] = 0, rest as infinity
  2. For each i, update reachable positions
  3. Answer is dp[n-1]

Time & Space Complexity

Time: O(n²)
Space: O(n)

C++ Implementation

#include 
#include 
using namespace std;

int main() {
    int arr[] = {2,3,1,1,4};
    int n = 5;
    int dp[n];

    dp[0] = 0;
    for(int i = 1; i < n; i++)
        dp[i] = INT_MAX;

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

    cout << (dp[n-1] == INT_MAX ? -1 : dp[n-1]);
}

Output:

2

Java Implementation

import java.util.Arrays;

class Main {
    public static void main(String[] args) {
        int[] arr = {2,3,1,1,4};
        int n = arr.length;
        int[] dp = new int[n];

        Arrays.fill(dp, Integer.MAX_VALUE);
        dp[0] = 0;

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

        System.out.print(dp[n-1] == Integer.MAX_VALUE ? -1 : dp[n-1]);
    }
}

Output:

2

Python Implementation

import sys

arr = [2,3,1,1,4]
n = len(arr)
dp = [sys.maxsize] * n
dp[0] = 0

for i in range(n):
    if dp[i] == sys.maxsize or arr[i] == 0:
        continue
    for j in range(1, arr[i] + 1):
        if i + j < n:
            dp[i + j] = min(dp[i + j], dp[i] + 1)

print(-1 if dp[-1] == sys.maxsize else dp[-1])

Output:

2

C# Implementation

using System;

class Program {
    static void Main() {
        int[] arr = {2,3,1,1,4};
        int n = arr.Length;
        int[] dp = new int[n];

        for(int i = 1; i < n; i++)
            dp[i] = int.MaxValue;

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

        Console.Write(dp[n - 1] == int.MaxValue ? -1 : dp[n - 1]);
    }
}

Output:

2

Approach 3: Greedy (Optimal)

Idea

Track:

  • maxReach → farthest index reachable
  • steps → steps left in current jump
  • jumps → number of jumps

Algorithm

  1. Initialize maxReach = arr[0], steps = arr[0], jumps = 1
  2. Traverse array
  3. Update max reach
  4. When steps become 0, take a jump

Time & Space Complexity

Time: O(n)
Space: O(1)

C++ Implementation

#include 
using namespace std;

int main() {
    int arr[] = {2,3,1,1,4};
    int n = 5;

    if(n <= 1) return 0;
    if(arr[0] == 0) {
        cout << -1;
        return 0;
    }

    int jumps = 1, maxReach = arr[0], steps = arr[0];

    for(int i = 1; i < n; i++) {
        if(i == n - 1) {
            cout << jumps;
            return 0;
        }

        maxReach = max(maxReach, i + arr[i]);
        steps--;

        if(steps == 0) {
            jumps++;
            if(i >= maxReach) {
                cout << -1;
                return 0;
            }
            steps = maxReach - i;
        }
    }
    cout << -1;
}

Output

2

Java Implementation

class Main {
    public static void main(String[] args) {
        int[] arr = {2,3,1,1,4};
        int n = arr.length;

        if(n <= 1) {
            System.out.print(0);
            return;
        }
        if(arr[0] == 0) {
            System.out.print(-1);
            return;
        }

        int jumps = 1, maxReach = arr[0], steps = arr[0];

        for(int i = 1; i < n; i++) {
            if(i == n - 1) {
                System.out.print(jumps);
                return;
            }

            maxReach = Math.max(maxReach, i + arr[i]);
            steps--;

            if(steps == 0) {
                jumps++;
                if(i >= maxReach) {
                    System.out.print(-1);
                    return;
                }
                steps = maxReach - i;
            }
        }
        System.out.print(-1);
    }
}

Output

2

Python Implementation

arr = [2,3,1,1,4]
n = len(arr)

if n <= 1:
    print(0)
elif arr[0] == 0:
    print(-1)
else:
    jumps = 1
    maxReach = arr[0]
    steps = arr[0]

    for i in range(1, n):
        if i == n - 1:
            print(jumps)
            break

        maxReach = max(maxReach, i + arr[i])
        steps -= 1

        if steps == 0:
            jumps += 1
            if i >= maxReach:
                print(-1)
                break
            steps = maxReach - i

Output

2

C# Implementation

using System;

class Program {
    static void Main() {
        int[] arr = {2,3,1,1,4};
        int n = arr.Length;

        if(n <= 1) {
            Console.Write(0);
            return;
        }
        if(arr[0] == 0) {
            Console.Write(-1);
            return;
        }

        int jumps = 1, maxReach = arr[0], steps = arr[0];

        for(int i = 1; i < n; i++) {
            if(i == n - 1) {
                Console.Write(jumps);
                return;
            }

            maxReach = Math.Max(maxReach, i + arr[i]);
            steps--;

            if(steps == 0) {
                jumps++;
                if(i >= maxReach) {
                    Console.Write(-1);
                    return;
                }
                steps = maxReach - i;
            }
        }
        Console.Write(-1);
    }
}

Output

2

Summary

  • Brute force is exponential and impractical
  • DP improves but still slow for large inputs
  • Greedy solution is optimal and interview-preferred
  • Greedy solves the problem in a single pass

Next Problem in the Series

Chocolate Distribution Problem


 

Sanjiv
0

You must logged in to post comments.

Related Blogs

Find the S...
Arrays February 02 ,2026

Find the Second Smal...

Find the S...
Arrays January 01 ,2026

Find the Sum of All...

Find the M...
Arrays January 01 ,2026

Find the Maximum Ele...

Find the M...
Arrays January 01 ,2026

Find the Minimum Ele...

Count Even...
Arrays January 01 ,2026

Count Even and Odd N...

Search an...
Arrays January 01 ,2026

Search an Element in...

Copy One A...
Arrays January 01 ,2026

Copy One Array into...

Reverse an...
Arrays January 01 ,2026

Reverse an Array

Print Alte...
Arrays January 01 ,2026

Print Alternate Elem...

Find the L...
Arrays January 01 ,2026

Find the Length of a...

Check if a...
Arrays January 01 ,2026

Check if an Array is...

Find the F...
Arrays January 01 ,2026

Find the First Eleme...

Find the L...
Arrays January 01 ,2026

Find the Last Elemen...

Count the...
Arrays January 01 ,2026

Count the Number of...

Replace Al...
Arrays January 01 ,2026

Replace All Elements...

Sum of Ele...
Arrays January 01 ,2026

Sum of Elements at E...

Sum of Ele...
Arrays January 01 ,2026

Sum of Elements at O...

Find the A...
Arrays January 01 ,2026

Find the Average of...

Count the...
Arrays January 01 ,2026

Count the Number of...

Remove Dup...
Arrays January 01 ,2026

Remove Duplicate Ele...

Move All Z...
Arrays January 01 ,2026

Move All Zeros to th...

Rotate an...
Arrays January 01 ,2026

Rotate an Array by K...

Rotate an...
Arrays January 01 ,2026

Rotate an Array by O...

Check if T...
Arrays January 01 ,2026

Check if Two Arrays...

Merge Two...
Arrays January 01 ,2026

Merge Two Sorted Arr...

Find Missi...
Arrays January 01 ,2026

Find Missing Number...

Find Dupli...
Arrays January 01 ,2026

Find Duplicate Eleme...

Count Freq...
Arrays January 01 ,2026

Count Frequency of E...

Find the M...
Arrays January 01 ,2026

Find the Majority El...

Find All U...
Arrays January 01 ,2026

Find All Unique Elem...

Insert an...
Arrays January 01 ,2026

Insert an Element at...

Delete an...
Arrays January 01 ,2026

Delete an Element fr...

Find the I...
Arrays January 01 ,2026

Find the Index of an...

Find Union...
Arrays January 01 ,2026

Find Union of Two Ar...

Find Inter...
Arrays January 01 ,2026

Find Intersection of...

Sort an Ar...
Arrays January 01 ,2026

Sort an Array of 0s...

Find the L...
Arrays January 01 ,2026

Find the Largest Sum...

Kadane’s A...
Arrays January 01 ,2026

Kadane’s Algorithm (...

Two Sum Pr...
Arrays January 01 ,2026

Two Sum Problem

Subarray w...
Arrays January 01 ,2026

Subarray with Given...

Longest Su...
Arrays January 01 ,2026

Longest Subarray wit...

Rearrange...
Arrays January 01 ,2026

Rearrange Array Alte...

Leaders in...
Arrays January 01 ,2026

Leaders in an Array

Equilibriu...
Arrays January 01 ,2026

Equilibrium Index of...

Stock Buy...
Arrays January 01 ,2026

Stock Buy and Sell (...

Stock Buy...
Arrays January 01 ,2026

Stock Buy and Sell (...

Sort an Ar...
Arrays January 01 ,2026

Sort an Array of 0s,...

Find the M...
Arrays January 01 ,2026

Find the Majority El...

Find All P...
Arrays January 01 ,2026

Find All Pairs with...

Longest Co...
Arrays January 01 ,2026

Longest Consecutive...

Product of...
Arrays January 01 ,2026

Product of Array Exc...

Maximum Pr...
Arrays January 01 ,2026

Maximum Product Suba...

Find the F...
Arrays January 01 ,2026

Find the First Missi...

Count Inve...
Arrays January 01 ,2026

Count Inversions in...

Rearrange...
Arrays January 01 ,2026

Rearrange Array by S...

Check if A...
Arrays January 01 ,2026

Check if Array Can B...

Trapping R...
Arrays January 01 ,2026

Trapping Rain Water

Find Minim...
Arrays January 01 ,2026

Find Minimum in Rota...

Search in...
Arrays January 01 ,2026

Search in Rotated So...

Median of...
Arrays January 01 ,2026

Median of Two Sorted...

Merge Inte...
Arrays January 01 ,2026

Merge Intervals

Count Reve...
Arrays January 01 ,2026

Count Reverse Pairs

Longest Su...
Arrays January 01 ,2026

Longest Subarray wit...

Largest Re...
Arrays January 01 ,2026

Largest Rectangle in...

Maximum Su...
Arrays January 01 ,2026

Maximum Sum Rectangl...

Subarray S...
Arrays January 01 ,2026

Subarray Sum Equals...

Count Dist...
Arrays January 01 ,2026

Count Distinct Eleme...

Sliding Wi...
Arrays January 01 ,2026

Sliding Window Maxim...

Find K Max...
Arrays January 01 ,2026

Find K Maximum Eleme...

Chocolate...
Arrays January 01 ,2026

Chocolate Distributi...

Find All T...
Arrays January 01 ,2026

Find All Triplets Wi...

Kth Smalle...
Arrays January 01 ,2026

Kth Smallest Element...

Maximum Le...
Arrays January 01 ,2026

Maximum Length Biton...

Find the S...
Arrays February 02 ,2026

Find the Second Larg...

Get In Touch

Kurki bazar Uttar Pradesh

+91-8808946970

techiefreak87@gmail.com