what would be the answer for this test case
4
0 2 3 1 ?
Minimum Jumps Required1
and how can it be optimised further?
Hello @archit_18,
Though this question doesn’t support a testcase like this, you can print -1 or any appropriate statement to indicate unreachable situation:
As we are starting from first position, and in above test case the jumps at that position is 0.
So, the program will stuck at that position.
If you are asking about an optimized way of solving this question:
Then you can use dynamic programming for the same as,
- Build a jumps[] array from left to right such that jumps[i] indicates the minimum number of jumps needed to reach arr[i] from arr[0].
- Finally, return jumps[n-1].
Hope, this would help.
Give a like, if you are satisfied.
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.