Minimize the sum of roots of a given polynomial | Javascript 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
function getMinimumSum ( arr , n ) { let res = [ ] ; let pos = [ ] ; let neg = [ ] ; for ( let i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > 0 ) pos . push ( i ) ; else if ( arr [ i ] < 0 ) neg . push ( i ) ; } if ( pos . length >= 2 && neg . length >= 2 ) { let posMax = Number . MIN_VALUE , posMaxIdx = - 1 ; let posMin = Number . MAX_VALUE , posMinIdx = - 1 ; let negMax = Number . MIN_VALUE , negMaxIdx = - 1 ; let negMin = Number . MAX_VALUE , negMinIdx = - 1 ; for ( let i = 0 ; i < pos . length ; i ++ ) { if ( arr [ pos [ i ] ] > posMax ) { posMaxIdx = pos [ i ] ; posMax = arr [ posMaxIdx ] ; } } for ( let i = 0 ; i < pos . length ; i ++ ) { if ( arr [ pos [ i ] ] < posMin && pos [ i ] != posMaxIdx ) { posMinIdx = pos [ i ] ; posMin = arr [ posMinIdx ] ; } } 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 ] ) ; } } let posVal = - 1.0 * posMax / posMin ; let negVal = - 1.0 * negMax / negMin ; if ( posVal < negVal ) { res . push ( arr [ posMinIdx ] ) ; res . push ( arr [ posMaxIdx ] ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( i != posMinIdx && i != posMaxIdx ) { res . push ( arr [ i ] ) ; } } } else { 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 ( " " ) ; } else if ( pos . length >= 2 ) { let posMax = Number . MIN_VALUE , posMaxIdx = - 1 ; let posMin = Number . MAX_VALUE , posMinIdx = - 1 ; for ( let i = 0 ; i < pos . length ; i ++ ) { if ( arr [ pos [ i ] ] > posMax ) { posMaxIdx = pos [ i ] ; posMax = arr [ posMaxIdx ] ; } } for ( let i = 0 ; i < pos . length ; i ++ ) { if ( arr [ pos [ i ] ] < posMin && pos [ i ] != posMaxIdx ) { posMinIdx = pos [ i ] ; posMin = arr [ posMinIdx ] ; } } res . push ( arr [ posMinIdx ] ) ; res . push ( arr [ posMaxIdx ] ) ; for ( let i = 0 ; i < n ; i ++ ) { if ( i != posMinIdx && i != posMaxIdx ) { res . push ( arr [ i ] ) ; } } for ( let i = 0 ; i < res . length ; i ++ ) { document . write ( res [ i ] + " " ) ; } document . write ( " " ) ; } 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 ( " " ) ; } else { document . write ( " " ) ; } } let arr = [ - 4 , 1 , 6 , - 3 , - 2 , - 1 ] ; let n = arr . length ; getMinimumSum ( arr , n ) ;