Case - 3 :
else if ( neg . length >= 2 ) { let negMax = Number . MIN_VALUE , negMaxIdx = - 1 ; let negMin = Number . MAX_VALUE , negMinIdx = - 1 ; for ( let i = 0 ; i < neg . length ; i ++ ) { if ( Math . abs ( arr [ neg [ i ] ] ) > negMax ) { negMaxIdx = neg [ i ] ; negMax = Math . abs ( arr [ negMaxIdx ] ) ; } } for ( let i = 0 ; i < neg . length ; i ++ ) { if ( Math . abs ( arr [ neg [ i ] ] ) < negMin && neg [ i ] != negMaxIdx ) { negMinIdx = neg [ i ] ; negMin = Math . abs ( arr [ negMinIdx ] ) ; } } res . push ( arr [ negMinIdx ] ) ; res . push ( arr [ negMaxIdx ] ) ; for ( let i = 0 ; i < n ; i ++ ) if ( i != negMinIdx && i != negMaxIdx ) res . push ( arr [ i ] ) ; for ( let i = 0 ; i < res . length ; i ++ ) document . write ( res [ i ] + " " ) ; document . write ( " " ) ; }