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


import_contacts

You may also like:

Related Posts

  • Maximum number of vowels in a Substring of given length
    18 June

    Maximum Number of Vowels in a Substring of Given Length

    Table of ContentsApproach – 1 Generate All Substrings Using substring() MethodApproach – 2 Using Sliding Window Method (Linear Time Solution) In this article, we will look at an interesting problem related to the Strings and [Sliding-Window Algorithm](https://java2blog.com/sliding-window-maximum-java/ “Sliding-Window Algorithm”). The problem is : "Given a String we have to Find the Maximum Number of Vowel […]

  • Find first and last position of element in sorted array
    04 June

    Search for a range Leetcode – Find first and last position of element in sorted array

    Table of ContentsApproach 1 (Using Linear Search)Approach 2 (Using Modified Binary Search-Optimal) In this article, we will look into an interesting problem asked in Coding Interviews related to Searching Algorithms. The problem is: Given a Sorted Array, we need to find the first and last position of an element in Sorted array. This problem is […]

  • 30 April

    Convert Postfix to Infix in Java

    Learn about how to convert Postfix to Infix in java.

  • Prefix to postfix in java
    30 April

    Convert Prefix to Postfix in Java

    Learn about how to convert Prefix to Postfix in java.

  • Data structures in java
    16 April

    Data Structures in java

    Table of ContentsArray Declare and initialize array in javaAdvantages of arrayDisadvantages of array ExampleArray practice programsStackStack implementation using ArrayStack implementation using LinkedListImplementationPractice ProgramsQueueQueue implementation using arrayQueue implementation using LinkedListImplementationLinkedListImplementationLinkedList Practice ProgramsBinary treeImplementationBinary tree practice programsBinary Search treeImplementationBinary search tree Practice programsTrieImplementationHeapImplementationGraphImplementation Inbuild data structures in javaStringHashMapLinkedHashMapArrayListLinkedListHashSet In this post, we will see about various data […]

  • 29 November

    Top 100+ Java coding interview questions

    Table of ContentsStringQuestion 1 : How to reverse a String in java? Can you write a program without using any java inbuilt methods?Question 2 : Write a java program to check if two Strings are anagram in java?Question 3 : Write a program to check if String has all unique characters in java?Question 4 : […]

Leave a Reply

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

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.