Minimize the sum of roots of a given polynomial | Java program to find minimum sum of roots of a given polynomial ; resultant vector ; a vector that store indices of the positive elements ; a vector that store indices of the negative elements ; Case - 1 : ; Case - 2 : ; Case - 3 : ; Case - 4 : ; Driver code
import java . io . * ; import java . util . * ; class GFG { static void getMinimumSum ( int arr [ ] , int n ) { ArrayList < Integer > res = new ArrayList < Integer > ( ) ; ArrayList < Integer > pos = new ArrayList < Integer > ( ) ; ArrayList < Integer > neg = new ArrayList < Integer > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > 0 ) pos . add ( i ) ; else if ( arr [ i ] < 0 ) neg . add ( i ) ; } if ( pos . size ( ) >= 2 && neg . size ( ) >= 2 ) { int posMax = Integer . MIN_VALUE , posMaxIdx = - 1 ; int posMin = Integer . MAX_VALUE , posMinIdx = - 1 ; int negMax = Integer . MIN_VALUE , negMaxIdx = - 1 ; int negMin = Integer . MAX_VALUE , negMinIdx = - 1 ; for ( int i = 0 ; i < pos . size ( ) ; i ++ ) { if ( arr [ pos . get ( i ) ] > posMax ) { posMaxIdx = pos . get ( i ) ; posMax = arr [ posMaxIdx ] ; } } for ( int i = 0 ; i < pos . size ( ) ; i ++ ) { if ( arr [ pos . get ( i ) ] < posMin && pos . get ( i ) != posMaxIdx ) { posMinIdx = pos . get ( i ) ; posMin = arr [ posMinIdx ] ; } } 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 ] ) ; } } double posVal = - 1.0 * ( double ) posMax / ( double ) posMin ; double negVal = - 1.0 * ( double ) negMax / ( double ) negMin ; if ( posVal < negVal ) { res . add ( arr [ posMinIdx ] ) ; res . add ( arr [ posMaxIdx ] ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( i != posMinIdx && i != posMaxIdx ) { res . add ( arr [ i ] ) ; } } } else { 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 . print ( res . get ( i ) + " ▁ " ) ; } System . out . println ( ) ; } else if ( pos . size ( ) >= 2 ) { int posMax = Integer . MIN_VALUE , posMaxIdx = - 1 ; int posMin = Integer . MAX_VALUE , posMinIdx = - 1 ; for ( int i = 0 ; i < pos . size ( ) ; i ++ ) { if ( arr [ pos . get ( i ) ] > posMax ) { posMaxIdx = pos . get ( i ) ; posMax = arr [ posMaxIdx ] ; } } for ( int i = 0 ; i < pos . size ( ) ; i ++ ) { if ( arr [ pos . get ( i ) ] < posMin && pos . get ( i ) != posMaxIdx ) { posMinIdx = pos . get ( i ) ; posMin = arr [ posMinIdx ] ; } } res . add ( arr [ posMinIdx ] ) ; res . add ( arr [ posMaxIdx ] ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( i != posMinIdx && i != posMaxIdx ) { res . add ( arr [ i ] ) ; } } for ( int i = 0 ; i < res . size ( ) ; i ++ ) { System . out . print ( res . get ( i ) + " ▁ " ) ; } System . out . println ( ) ; } 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 ( ) ; } else { System . out . println ( " No ▁ swap ▁ required " ) ; } } public static void main ( String [ ] args ) { int arr [ ] = { - 4 , 1 , 6 , - 3 , - 2 , - 1 } ; int n = arr . length ; getMinimumSum ( arr , n ) ; } }