top of page

Minimum Number of Jumps to Reach End

  • Shreyas Naphad
  • Jul 24, 2024
  • 1 min read

In this article, we will solve the problem of finding the minimum number of jumps required to reach the end of an array. This simply means that we have an array of integers where every integer represents the maximum number of steps that can be made forward from that element.


So we have to make a function to return the minimum number of jumps needed to reach the end.

Example:

Input: array = [2, 3, 1, 1, 4]

Output: 2

Explanation: The minimum number of jumps to reach the end is 2 (2 -> 3 -> 4).

 

Solution:

To solve this problem, we will use a greedy approach. We'll keep track of the farthest point that can be reached at each step and the number of jumps required to reach the end.


Steps to Solve:

1.     Initialize Variables:

o    maxReach to keep track of the farthest point that can be reached.

o    steps to keep track of the number of steps we can still take.

o    jumps to keep track of the number of jumps taken so far.

2.     Iterate Through the Array:

o    For each element in the array:

§  Update maxReach to the maximum index that can be reached.

§  Decrement steps (since we used one step to move to the current element).

§  If steps becomes zero, increment jumps and update steps to the number of steps to reach maxReach.

3.     Check for Failure:

o    If at any point, the current index is greater than or equal to maxReach and we haven't reached the end, return -1.



Time Complexity: O(N)

Space Complexity: O(1)

Comments


©2025 by DevSparks.

bottom of page