Maximize every array element by repeatedly adding all valid i + a [ i ] th array element | Function to maximize value at every array index by performing given operations ; Traverse the array in reverse ; If the current index is a valid index ; Print the array ; Given array ; Size of the array
function maxSum ( arr , N ) { let ans = 0 ; for ( let i = N - 1 ; i >= 0 ; i -- ) { let t = i ; if ( t + arr [ i ] < N ) { arr [ i ] += arr [ t + arr [ i ] ] ; } } for ( let i = 0 ; i < N ; i ++ ) { document . write ( arr [ i ] + ' ' ) ; } } let arr = [ 1 , 2 , 7 , 1 , 8 ] ; let N = arr . length ; maxSum ( arr , N ) ;