Maximize every array element by repeatedly adding all valid i + a [ i ] th array element | C # program for the above approach ; 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 ; Driver Code ; Given array ; Size of the array
using System ; class GFG { static void maxSum ( int [ ] arr , int N ) { for ( int i = N - 1 ; i >= 0 ; i -- ) { int t = i ; if ( t + arr [ i ] < N ) { arr [ i ] += arr [ t + arr [ i ] ] ; } } for ( int i = 0 ; i < N ; i ++ ) { Console . Write ( arr [ i ] + " ▁ " ) ; } } static public void Main ( ) { int [ ] arr = { 1 , 2 , 7 , 1 , 8 } ; int N = arr . Length ; maxSum ( arr , N ) ; } }