Find the maximum element in the array other than Ai | Function to find maximum element among ( N - 1 ) elements other than a [ i ] for each i from 1 to N ; To store prefix max element ; To store suffix max element ; Find the maximum element in the array other than a [ i ] ; Driver code
function max_element ( a , n ) { let pre = new Array ( n ) ; pre [ 0 ] = a [ 0 ] ; for ( let i = 1 ; i < n ; i ++ ) pre [ i ] = Math . max ( pre [ i - 1 ] , a [ i ] ) ; let suf = new Array ( n ) ; suf [ n - 1 ] = a [ n - 1 ] ; for ( let i = n - 2 ; i >= 0 ; i -- ) suf [ i ] = Math . max ( suf [ i + 1 ] , 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 ] ) + " " ) ; } } let a = [ 2 , 5 , 6 , 1 , 3 ] ; let n = a . length ; max_element ( a , n ) ;