Check if it is possible to reach end of given Array by Jumping

Table of Contents

If you want to practice data structure and algorithm programs, you can go through Java coding interview questions.






Problem

Given an array with positive integers as elements indicating the maximum length of a jump which can be made from any position in the array. Check if it is possible to have a jumps combination so that you can eventually reach the end of given array. Print "true" if it is possible, otherwise, print "false".

Input :
[1, 5, 2, 1, 0, 2, 0]

Output :
true

Input :
[5, 4, 3, 2, 1, 0, 0]

Output :
false


Solution

APPROACH – I (RECURSIVE)

This a very basic brute force approach in which we make all the possible jumps from any current position.
We start with the current position at index zero in the array, and make a recursive call by changing the current position by adding all the possible jump lengths one by one. We also need to make sure that we stay in bounds of the given array, that is why we only make the jump if the current position after adding this jump length stays in bound.

Positive Base case : If the current position is equal to array.length, this means that we have successfully reached the end of the given array, that is, a jumps combination exists with which we can reach end of given array, hence we return true from here.

Negative base case : if maximum length of any jump possible from any current position is equal to zero, this means that there is no way possible with which we can reach the end of the given array as we cannot move even a single position from our current position.

Time complexity Discussion :

As from every point we are making all the possible jumps and in worst case from any position, we can make ‘n’ jumps and there are n possible spots from where we can make these jumps, therefore, worst time complexity of this algorithm for solving this problem is O(n^n), where ‘n’ is the length of the given array.

APPROACH – II (Iterative) :

In the previous approach we can clearly see so many duplications of recursive call which find the result of same position again and again, therefore, we can use Dynamic Programming to solve the given problem and also reduce the running time complexity of the algorithm.

In this Dynamic Programming approach, we calculate result for every index that is, if it is possible to reach to that ‘i’th index from 0th index with a combination of possible jumps.

Therefore, we make a boolean DP array for storing the result for every index which is false initially except the first index as this is where we start the journey from.

  • Some Points which are needed to be considered are :
    (i) We can make further jumps from any position only if that position itself is reachable from Zeroth index.
    (ii) We can not move any further from an index if the maximum allowed length of any jump is Zero.
    Therefore, we can only move from any position only if that position itself is reachable and length of jump allowed from that position is not zero.

 

Our main motive is to find if there exists a path or jump combination from Zeroth index to Last index.

  • To prove this, we can agree on the fact that, If there exist a path or jump combination from Zeroth index to any Intermediate Index, and also there exist a path or jump combination from Intermediate index to Last Index, Then the the last index will surely be reachable from Zeroth index.

Time complexity Discussion :
As in this approach also we are making jumps of all possible lengths which can potentially be ‘n’ in worst case, and we are doing this in loop for every index of the array, then, the worst time complexity of this algorithm for solving this problem is O(n^2), where ‘n’ is length of the given array.

That’s all about to check if it is possible to reach end of given Array

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *