Not sure why a greedy approach for the problem Minimum Jumps required work

I am not sure why a greedy approach for the above mentioned problem work. If I start from index 0 of array and jump to the maximum elements (till arr[0]) and continue like this till I reach index n, why wouldn’t that be the minimum number of jumps.

Greedy code for reference: https://ide.codingblocks.com/s/461146

hello @Yash5646

if we are currently at i.

then using i we can clearly cover
[i…i+a[i]] range of indices, so what should be our next step from here(from i )?
we need to jump to that index among [i…i+a[i] ] , that will expand the range/reachability furthur, now which is the best best candidate?
it should be maximum among a[j] where j=[i…i+a[i] ] right?

now while implementation we need to take care of cases where no solution exist ( u r not handling). while making a jump we need to make sure i+jump distance not exceed the indices range (otherwise it will give segfault, u r not handing inside the inner loop).

check this for clarity- >link

1 Like

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.