I am unable to get this approach
base cases:
if there is no element or only one element no jump is needed
if the first element is zero, you cannot reach any other element so no answer is possible
maxreach stores maximum reachable index from any given position and hence initialised to arr[0]
suppose if arr[0] = 1 u can reach maximum to 1
step stores how many more jumps can u take, this is to try and reach each state from a given state and reach the maximum
in the loop
if at any point the last index is reached u simply return the jumps taken so far remember all the base cases so to reach even the next to first element u need atleast one jump and hence initialised to 1
maxreach is updated to the maximum of current maxreach and the next
consider 3 1 2 4 5 intially max reach is 3 for i = 1 it will try change to 1 + arr[i] which is 2 so it will not be updated for just the next case it will updated for i = 2 it basically counts the next maximum index u can reach
step is decremented because to reach the current state u must take atleast one step ahead from all the steps available, it was initialised to arr[0]
if this step is now 0, that means u are at maximum reach from a given state and since the loop is not over that means u have still not reached the end of the array
so u must definitely now take a jump
now we check if the current state is the maximum for all the previous states, if it is true this would mean that i was maximum and you cannot reach any further and since this is not the end and u cannot reach any further, it is a stalemate and hence return -1
step = maxreach - i means u r ate ith state and to reach the next max state, how many steps do u need? maxreach - i steps so it is changed to that