Given an array of non-negative integers, where each array element represents the maximum number of positions one can move forward from that element. Find the minimum number of jumps required to reach a given destination from a given source within the array.

If any element has value zero in the array, the destination cannot be reached through that element. If the source itself has value zero, return infinity as the destination cannot be reached at all. To make the problem simpler, let’s assume the source and destination to be the start and end of the array.

 
For example,

Input:  nums[] = { 4, 2, 0, 3, 2, 0, 1, 8 }
 
Output: Minimum jumps required to reach the destination are 3.
 
3 jumps: (4 —> 3 —> 1 —> 8) or (4 —> 2 —> 1 —> 8)
4 jumps: (4 —> 2 —> 3 —> 1 —> 8) or (4 —> 3 —> 2 —> 1 —> 8)
5 jumps: (4 —> 2 —> 3 —> 2 —> 1 —> 8)
 
 
Input:  nums[] = { 4, 2, 2, 1, 0, 8, 1 }
 
Output: Minimum jumps required to reach the destination are infinity. This is because no matter what path we choose, we will always end up in a dead cell.
 
4 —> 2 —> 2 —> 1 —> 0
4 —> 2 —> 1 —> 0
4 —> 1 —> 0
4 —> 0

Practice this problem

The idea is to recur for all elements reachable from the source and consider their minimum cost. The recurrence relation T(n) can be written as:

T(source, dest) = minimum{T(j, dest)} for all j reachable from source

The time complexity of this solution would be exponential since we might end up computing the same subproblem repeatedly. We can use dynamic programming to optimize the code since this problem exhibits both properties of dynamic programming, i.e., overlapping subproblems and optimal substructure.

1. Using Memoization

We can use memoization to solve this problem in a top-down fashion. The idea is to store the results of function calls and return the cached result when the same inputs occur again.

C


Download  Run Code

Output:

The minimum jumps required to reach the destination are 3

Java


Download  Run Code

Output:

The minimum jumps required to reach the destination are 3

Python


Download  Run Code

Output:

The minimum jumps required to reach the destination are 3

The time complexity of the above top-down solution is O(n3) and requires O(n2) extra space, where n is the size of the input.

2. Using Tabulation

Another idea is to construct an auxiliary array lookup[] for storing the subproblem solutions. For an array nums[], lookup[i] will store the minimum jumps required to reach nums[i] from source nums[0]. The algorithm can be implemented as follows in C, Java, and Python, where lookup[] is filled in a bottom-up fashion.

C


Download  Run Code

Output:

The minimum jumps required to reach the destination are 3

Java


Download  Run Code

Output:

The minimum jumps required to reach the destination are 3

Python


Download  Run Code

Output:

The minimum jumps required to reach the destination are 3

The time complexity of the above bottom-up solution is O(n2) and requires O(n) extra space, where n is the size of the input.