Maximum jumps to reach end of Array with condition that index i can make arr [ i ] jumps | C ++ code for the above approach ; 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
#include <bits/stdc++.h> NEW_LINE using namespace std ; void findMaxJumps ( int arr [ ] , int N ) { int dp [ N ] = { 0 } ; int ans = 0 ; for ( int i = N - 1 ; i >= 0 ; i -- ) { dp [ i ] = arr [ i ] ; int j = i + arr [ i ] ; if ( j < N ) { dp [ i ] = dp [ i ] + dp [ j ] ; } ans = max ( ans , dp [ i ] ) ; } cout << ans ; } int main ( ) { int arr [ ] = { 2 , 3 , 5 , 7 , 9 } ; int N = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; findMaxJumps ( arr , N ) ; return 0 ; }