Minimize the sum of roots of a given polynomial | C # 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
using System ; using System . Collections . Generic ; class GFG { static void getMinimumSum ( int [ ] arr , int n ) { List < int > res = new List < int > ( ) ; List < int > pos = new List < int > ( ) ; List < int > neg = new List < int > ( ) ; for ( int i = 0 ; i < n ; i ++ ) { if ( arr [ i ] > 0 ) pos . Add ( i ) ; else if ( arr [ i ] < 0 ) neg . Add ( i ) ; } if ( pos . Count >= 2 && neg . Count >= 2 ) { int posMax = Int32 . MinValue , posMaxIdx = - 1 ; int posMin = Int32 . MaxValue , posMinIdx = - 1 ; int negMax = Int32 . MinValue , negMaxIdx = - 1 ; int negMin = Int32 . MaxValue , negMinIdx = - 1 ; for ( int i = 0 ; i < pos . Count ; i ++ ) { if ( arr [ pos [ i ] ] > posMax ) { posMaxIdx = pos [ i ] ; posMax = arr [ posMaxIdx ] ; } } for ( int i = 0 ; i < pos . Count ; i ++ ) { if ( arr [ pos [ i ] ] < posMin && pos [ i ] != posMaxIdx ) { posMinIdx = pos [ i ] ; posMin = arr [ posMinIdx ] ; } } for ( int i = 0 ; i < neg . Count ; i ++ ) { if ( Math . Abs ( arr [ neg [ i ] ] ) > negMax ) { negMaxIdx = neg [ i ] ; negMax = Math . Abs ( arr [ negMaxIdx ] ) ; } } for ( int i = 0 ; i < neg . Count ; i ++ ) { if ( Math . Abs ( arr [ neg [ i ] ] ) < negMin && neg [ i ] != negMaxIdx ) { negMinIdx = neg [ 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 . Count ; i ++ ) { Console . Write ( res [ i ] + " ▁ " ) ; } Console . WriteLine ( ) ; } else if ( pos . Count >= 2 ) { int posMax = Int32 . MinValue , posMaxIdx = - 1 ; int posMin = Int32 . MaxValue , posMinIdx = - 1 ; for ( int i = 0 ; i < pos . Count ; i ++ ) { if ( arr [ pos [ i ] ] > posMax ) { posMaxIdx = pos [ i ] ; posMax = arr [ posMaxIdx ] ; } } for ( int i = 0 ; i < pos . Count ; i ++ ) { if ( arr [ pos [ i ] ] < posMin && pos [ i ] != posMaxIdx ) { posMinIdx = pos [ 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 . Count ; i ++ ) { Console . Write ( res [ i ] + " ▁ " ) ; } Console . WriteLine ( ) ; } else if ( neg . Count >= 2 ) { int negMax = Int32 . MinValue , negMaxIdx = - 1 ; int negMin = Int32 . MaxValue , negMinIdx = - 1 ; for ( int i = 0 ; i < neg . Count ; i ++ ) { if ( Math . Abs ( arr [ neg [ i ] ] ) > negMax ) { negMaxIdx = neg [ i ] ; negMax = Math . Abs ( arr [ negMaxIdx ] ) ; } } for ( int i = 0 ; i < neg . Count ; i ++ ) { if ( Math . Abs ( arr [ neg [ i ] ] ) < negMin && neg [ i ] != negMaxIdx ) { negMinIdx = neg [ 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 . Count ; i ++ ) Console . WriteLine ( res [ i ] + " ▁ " ) ; Console . WriteLine ( ) ; } else { Console . WriteLine ( " No ▁ swap ▁ required " ) ; } } static public void Main ( ) { int [ ] arr = { - 4 , 1 , 6 , - 3 , - 2 , - 1 } ; int n = arr . Length ; getMinimumSum ( arr , n ) ; } }