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 ; Prthe array ; Driver Code ; Given array ; Size of the array
def maxSum ( arr , N ) : NEW_LINE INDENT ans = 0 ; NEW_LINE for i in range ( N - 1 , - 1 , - 1 ) : NEW_LINE INDENT t = i ; NEW_LINE if ( t + arr [ i ] < N ) : NEW_LINE INDENT arr [ i ] += arr [ t + arr [ i ] ] ; NEW_LINE DEDENT DEDENT for i in range ( N ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ 1 , 2 , 7 , 1 , 8 ] ; NEW_LINE N = len ( arr ) ; NEW_LINE maxSum ( arr , N ) ; NEW_LINE DEDENT