Modify array by replacing every array element with minimum possible value of arr [ j ] + | j | C # program for the above approach ; Function to find minimum value of arr [ j ] + | j - i | for every array index ; Stores minimum of a [ j ] + | i - j | upto position i ; Stores minimum of a [ j ] + | i - j | upto position i from the end ; Traversing and storing minimum of a [ j ] + | i - j | upto i ; Traversing and storing minimum of a [ j ] + | i - j | upto i from the end ; Traversing from [ 0 , N ] and storing minimum of a [ j ] + | i - j | from starting and end ; Print the required array ; Driver code ; Given array [ ] arr ; Size of the array ; Function Call
using System ; using System . Collections . Generic ; class GFG { static void minAtEachIndex ( int n , int [ ] arr ) { int [ ] dp1 = new int [ n ] ; int [ ] dp2 = new int [ n ] ; int i ; dp1 [ 0 ] = arr [ 0 ] ; for ( i = 1 ; i < n ; i ++ ) dp1 [ i ] = Math . Min ( arr [ i ] , dp1 [ i - 1 ] + 1 ) ; dp2 [ n - 1 ] = arr [ n - 1 ] ; for ( i = n - 2 ; i >= 0 ; i -- ) dp2 [ i ] = Math . Min ( arr [ i ] , dp2 [ i + 1 ] + 1 ) ; List < int > v = new List < int > ( ) ; for ( i = 0 ; i < n ; i ++ ) v . Add ( Math . Min ( dp1 [ i ] , dp2 [ i ] ) ) ; foreach ( int x in v ) Console . Write ( x + " ▁ " ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 1 , 4 , 2 , 5 , 3 } ; int N = arr . Length ; minAtEachIndex ( N , arr ) ; } }