Find the maximum element in the array other than a [ i ]
for ( let i = 0 ; i < n ; i ++ ) { if ( i == 0 ) document . write ( suf [ i + 1 ] + " " ) ; else if ( i == n - 1 ) document . write ( pre [ i - 1 ] + " " ) ; else document . write ( Math . max ( pre [ i - 1 ] , suf [ i + 1 ] ) + " " ) ; } }