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
def findMaxJumps ( arr , N ) : NEW_LINE INDENT dp = [ 0 for i in range ( N ) ] NEW_LINE ans = 0 NEW_LINE i = N - 1 NEW_LINE while ( i >= 0 ) : NEW_LINE INDENT dp [ i ] = arr [ i ] NEW_LINE j = i + arr [ i ] NEW_LINE if ( j < N ) : NEW_LINE INDENT dp [ i ] = dp [ i ] + dp [ j ] NEW_LINE DEDENT ans = max ( ans , dp [ i ] ) NEW_LINE i -= 1 NEW_LINE DEDENT print ( ans ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 2 , 3 , 5 , 7 , 9 ] NEW_LINE N = len ( arr ) NEW_LINE findMaxJumps ( arr , N ) NEW_LINE DEDENT