Minimum jumps problem


why is this giving wrong answer on some testcases.
plz correct the code …

reply at the earliest…

how to solve it in o(n) time complexity…plz provide the code and explaination…

hello @S19LPPP0202
for O(n)->
Now moving to optimised approach,basically idea is to take jump only when its needed. The variable maxReach stores at all time the maximal reachable position in the array. jump stores the amount of jumps necessary to reach that position. step stores the amount of steps we can still take (and is initialized with the amount of steps at the first array position)

During the iteration, the above values are updated as follows:

First we test whether we have reached the end of the array, in which case we just need to return the jump variable.

Next we update the maximal reachable position. This is equal to the maximum of maxReach and i+A[i] (the number of steps we can take from the current position).

We used up a step to get to the current index, so steps has to be decreased.

If no more steps are remaining (i.e. steps=0, then we must have used a jump. Therefore increase jump. Since we know that it is possible somehow to reach maxReach, we initialize the steps to the amount of steps to reach maxReach from position i.

In ur O(N^2).
add some edge cases like if n==0 return INT_MAX;
a[i]==0 return INT_MAX,
image

add one to it only when return value from recur function is not equal to INT_MAX
otherwise overflow will occur

add one to it only when return value from recur function is not equal to INT_MAX
otherwise overflow will occur

plz tell me how to do this in my code what should i write??so that when it encounters zero it returns int_max only for that one…

when u r calling recurr function then store the returned value in some variable (say x).

and if (x !=INT_MAX) then only add 1 and compare it with minimum

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.