Maximum jumps to reach end of Array with condition that index i can make arr [ i ] jumps | Function to find the maximum jumps to reach end of array ; Stores the jumps needed to reach end from each index ; Traverse the array ; Check if j is less than N ; Add dp [ j ] to the value of dp [ i ] ; Update the value of ans ; Print the value of ans ; Driver Code
function findMaxJumps ( arr , N ) { let dp = new Array ( N ) . fill ( 0 ) ; let ans = 0 ; for ( let i = N - 1 ; i >= 0 ; i -- ) { dp [ i ] = arr [ i ] ; let j = i + arr [ i ] ; if ( j < N ) { dp [ i ] = dp [ i ] + dp [ j ] ; } ans = Math . max ( ans , dp [ i ] ) ; } document . write ( ans ) ; } let arr = [ 2 , 3 , 5 , 7 , 9 ] ; let N = arr . length ; findMaxJumps ( arr , N ) ;