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".
[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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
package Arrays; import java.util.Scanner; public class reachEnd { public static void main(String[] args) { Scanner scn = new Scanner(System.in); /*input size of the array*/ int n = scn.nextInt(); int[]arr = new int[n]; /*input elements in the array*/ for(int i=0;i<arr.length; i++) { arr[i] = scn.nextInt(); } System.out.println(solve(0, arr)); } public static boolean solve(int cp, int[] arr) { /* base case : if the current position of the pointer * is at last element of the array which indicates * that we have reached the end of the array, then * we can return true from here*/ if (cp == arr.length-1) { return true; } /* if maximum jump which we can make from current * position is zero, then there is no way we can * reach end of array using the current combination * of jumps, hence return false from here */ if (arr[cp] == 0) { return false; } boolean rv = false; /* start from current position, and make all the jumps of * possible length, and make a recursive call which * eventually changes our current position */ for (int jump = 1; jump <= arr[cp]; jump++) { if(cp + jump < arr.length){ /* here we use logical 'or' operator because any true * result is sufficient enough to prove that a jumps * combination exists with which we can reach end of * given array*/ rv = rv || solve(cp + jump, arr); } } return rv; } } |
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
package Arrays; import java.util.Scanner; public class reachEnd { public static void main(String[] args) { Scanner scn = new Scanner(System.in); /* input size of the array */ int n = scn.nextInt(); int[] arr = new int[n]; /* input elements in the array */ for (int i = 0; i < arr.length; i++) { arr[i] = scn.nextInt(); } System.out.println( solve(arr)); } public static boolean solve(int[] arr) { /* dp array to store the result of every index, that is, * if they are reachable or not, true indicates that there * exists one or more jump combinations to reach that index, * and no jump combination exist in case of a false*/ boolean[] dp = new boolean[arr.length]; /* first index is always reachable, as this is the position * we start from*/ dp[0] = true; /*finding result for every index*/ for (int currPos = 0; currPos < arr.length; currPos++) { /* if the index we are currently at is not reachable from * 0th index, then we obviously can not make further jumps * from this position. * Also, number of jumps possible from the current position * needs to be greater than zero, as in case of zero, we can * not move to any other position by making a jump*/ if (dp [currPos] && arr [currPos] > 0) { int maxJumps = arr[currPos]; /* mark all the reachable positions from current position * true because, if they can be reached from an intermediate * spot, and that intermediate spot can be reached from zero, * then the jumped position will also be reachable from zeroth * index*/ for (int jump = 1; jump <= maxJumps; jump++) { if(currPos + jump < dp.length) { dp[currPos+jump] = true; } } } } /*return the result of last index of the array if it is reachable * from zeroth index or not*/ return dp [arr.length-1]; } } |
That’s all about to check if it is possible to reach end of given Array