Last Updated: 8 Sep, 2022

JUMP GAME

Moderate
Asked in companies
WalmartGrowwPolicyBazaar.com

Problem statement

Given an array of non-negative integers ‘ARR’ of length ‘N’, you are initially positioned at the array's first index.

Each element in the array represents your maximum jump length at that position.

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

If it is not possible to reach the last index, return -1.

Example:
Input:

‘N’ = 3
‘ARR’ = [ 2, 1, 1 ]

The shortest way to reach index 2 is
Index 0 => Index 2
that requires only 1 jump.
Input Format:
The first line contains ‘T,’ denoting the number of test cases.

The first line of each test case contains ‘N’ denoting the size of the ‘ARR’.

The second line of each test case contains 'N' space-separated Integers denoting the ‘ARR’.
Output Format:
For each test case, Return the answer as described in the problem statement.

Output for each test case is printed on a separate line.
Note :
You don't need to print anything. It has already been taken care of. Just implement the given function.
Constraints :
1 <= T <= 10    
1 <= N <= 10000
0 <= ARR [ i ] <= N
Time Limit: 1 sec

Approaches

01 Approach

We can use dynamic programming. We can build an array ‘DP’ of size ‘N’ where

DP [ i ] denotes the minimum number of jumps to reach index ‘i’ from index 0.

 

We can traverse the array from left to right for every index ‘i’ from 0 to ‘N’ - 1. For every index ‘i’,  we can iterate from index ‘i’ + 1 to + ‘i’ + ARR [ i ], updating the values if jumping from index ‘i’ is more optimal.

 

The steps are as follows:-

 

function minJumps ( int N , [ int ] ARR ):

  1. Initialise an array ‘DP’ of the size of ‘N’ with -1.
  2. Initialise DP [ 0 ] with 0.
  3. Run a loop from ‘i’ =0 to ‘N’ -1:
    • Run a loop from ‘j’ =’i’ + 1 to ‘i’ + ARR [ i ]:
      • If DP [ i ] is not -1 and either  DP [ i ] + 1 < DP [ j ] or DP [ j ] is -1
        • DP [ j ] = DP [ i ] + 1.
  4. Return DP [ N - 1 ].

02 Approach

From any jump point i, we can reach any index from (i + 1) to (i + A[i]), and there will be some value between range ARR [i + 1] to ARR [i + ARR[i]], which can provide the farthest reach from that range. 

So we define lower and upper end of the current jump point and calculate the farthest reach in that range. Whenever we reach the upper endpoint of a range, we update the upper end with the value of farthest reach and increment jump count. We continue this process till the value of farthest reachable index is greater than n - 1.

 

The steps are as follows:

 

function minJumps( int N, [ int ] ARR ):

  1. If ‘N’ <= 1
    • Return 0.
  2. Initialise variables ‘maxReachPos’ and ‘curMaxReachPos’ with     ARR [ 0 ].
  3. Initialise ‘currStep’ with 1.
  4. Run a loop from ‘i’ =1 to ‘maxReachPos’:
    • If i == ‘N’ -1:
      • Return currStep.
    • update ‘curMaxReachPos’ with max(curMaxReachPos, i + ARR[i]);
    • If(i == maxReachPos):
      • If (curMaxReachPos <= i)
        • return -1;
      • Update ‘maxReachPos’ with ‘curMaxReachPos’.
      • currStep++;
  5. return -1.