Logic behind min jump problem

min jump problem Given an array of integers where each element represents the max number of steps that can be made forward from that element. Print the minimum number of jumps to reach the end of the array (starting from the first element). If an element is 0, then cannot move through that element.

my solution


does my logic is correct
i searched for max step i can take form i th position if not what will logic of this problem

@bishtmanish786 Your code gives wrong answer for any test case. Follow this Dynamic Programming approach:

Hope this helps :slightly_smiling_face:

1 Like

@pratyush63 sir ye greedy se nhi hoga kya?

@bishtmanish786 I don’t thin so greedy will always work here and pass all test cases. You would have to check for all possible jumps to reach a particular position. By greedy if i would say that you would prefer to jump to that particular array position from where maximum jump is possible .
Say array is a[] = {1 ,5, 2, 0, 0, 0, 1,1}
Following your greedy approach: Currently at element 1, you will jump to 5.
From 5, you can jump to 2,0,0,0,1. If you go by greedy(ie. Always pick the largest number within the current range) , you will prefer to jump to 2 but that will lead to a 0 (ie. no possible solution). So you have to jump to 1, in order to reach the end of the array.
So, for this problem Dynamic programming approach leads to the standard solution.

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.

1 Like