Case - 3 :
else if ( neg . size ( ) >= 2 ) { int negMax = Integer . MIN_VALUE , negMaxIdx = - 1 ; int negMin = Integer . MAX_VALUE , negMinIdx = - 1 ; for ( int i = 0 ; i < neg . size ( ) ; i ++ ) { if ( Math . abs ( arr [ neg . get ( i ) ] ) > negMax ) { negMaxIdx = neg . get ( i ) ; negMax = Math . abs ( arr [ negMaxIdx ] ) ; } } for ( int i = 0 ; i < neg . size ( ) ; i ++ ) { if ( Math . abs ( arr [ neg . get ( i ) ] ) < negMin && neg . get ( i ) != negMaxIdx ) { negMinIdx = neg . get ( i ) ; negMin = Math . abs ( arr [ negMinIdx ] ) ; } } res . add ( arr [ negMinIdx ] ) ; res . add ( arr [ negMaxIdx ] ) ; for ( int i = 0 ; i < n ; i ++ ) if ( i != negMinIdx && i != negMaxIdx ) res . add ( arr [ i ] ) ; for ( int i = 0 ; i < res . size ( ) ; i ++ ) System . out . println ( res . get ( i ) + " ▁ " ) ; System . out . println ( ) ; }