Minimize the sum of roots of a given polynomial | Python3 program to find minimum sum of roots of a given polynomial ; resultant list ; a lis that store indices of the positive elements ; a list that store indices of the negative elements ; Case - 1 : ; Case - 2 : ; Case - 3 : ; Case - 4 : ; Driver code
import sys NEW_LINE def getMinimumSum ( arr , n ) : NEW_LINE INDENT res = [ ] NEW_LINE pos = [ ] NEW_LINE neg = [ ] NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( arr [ i ] > 0 ) : NEW_LINE INDENT pos . append ( i ) NEW_LINE DEDENT elif ( arr [ i ] < 0 ) : NEW_LINE INDENT neg . append ( i ) NEW_LINE DEDENT DEDENT if ( len ( pos ) >= 2 and len ( neg ) >= 2 ) : NEW_LINE INDENT posMax = - sys . maxsize - 1 NEW_LINE posMaxIdx = - 1 NEW_LINE posMin = sys . maxsize NEW_LINE posMinIdx = - 1 NEW_LINE negMax = - sys . maxsize - 1 NEW_LINE negMaxIdx = - 1 NEW_LINE negMin = sys . maxsize NEW_LINE negMinIdx = - 1 NEW_LINE for i in range ( len ( pos ) ) : NEW_LINE INDENT if ( arr [ pos [ i ] ] > posMax ) : NEW_LINE INDENT posMaxIdx = pos [ i ] NEW_LINE posMax = arr [ posMaxIdx ] NEW_LINE DEDENT DEDENT for i in range ( len ( pos ) ) : NEW_LINE INDENT if ( arr [ pos [ i ] ] < posMin and pos [ i ] != posMaxIdx ) : NEW_LINE INDENT posMinIdx = pos [ i ] NEW_LINE posMin = arr [ posMinIdx ] NEW_LINE DEDENT DEDENT for i in range ( len ( neg ) ) : NEW_LINE INDENT if ( abs ( arr [ neg [ i ] ] ) > negMax ) : NEW_LINE INDENT negMaxIdx = neg [ i ] NEW_LINE negMax = abs ( arr [ negMaxIdx ] ) NEW_LINE DEDENT DEDENT for i in range ( len ( neg ) ) : NEW_LINE INDENT if ( abs ( arr [ neg [ i ] ] ) < negMin and neg [ i ] != negMaxIdx ) : NEW_LINE INDENT negMinIdx = neg [ i ] NEW_LINE negMin = abs ( arr [ negMinIdx ] ) NEW_LINE DEDENT DEDENT posVal = ( - 1.0 * posMax / posMin ) NEW_LINE negVal = ( - 1.0 * negMax / negMin ) NEW_LINE if ( posVal < negVal ) : NEW_LINE INDENT res . append ( arr [ posMinIdx ] ) NEW_LINE res . append ( arr [ posMaxIdx ] ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( i != posMinIdx and i != posMaxIdx ) : NEW_LINE INDENT res . append ( arr [ i ] ) NEW_LINE DEDENT DEDENT DEDENT else : NEW_LINE INDENT res . append ( arr [ negMinIdx ] ) NEW_LINE res . append ( arr [ negMaxIdx ] ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( i != negMinIdx and i != negMaxIdx ) : NEW_LINE INDENT resal . append ( arr [ i ] ) NEW_LINE DEDENT DEDENT DEDENT for i in range ( len ( res ) ) : NEW_LINE INDENT print ( res [ i ] , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT elif ( len ( pos ) >= 2 ) : NEW_LINE INDENT posMax = - sys . maxsize NEW_LINE posMaxIdx = - 1 NEW_LINE posMin = sys . maxsize NEW_LINE posMinIdx = - 1 NEW_LINE for i in range ( len ( pos ) ) : NEW_LINE INDENT if ( arr [ pos [ i ] ] > posMax ) : NEW_LINE INDENT posMaxIdx = pos [ i ] NEW_LINE posMax = arr [ posMaxIdx ] NEW_LINE DEDENT DEDENT for i in range ( len ( pos ) ) : NEW_LINE INDENT if ( arr [ pos [ i ] ] < posMin and pos [ i ] != posMaxIdx ) : NEW_LINE INDENT posMinIdx = pos [ i ] NEW_LINE posMin = arr [ posMinIdx ] NEW_LINE DEDENT DEDENT res . append ( arr [ posMinIdx ] ) NEW_LINE res . append ( arr [ posMaxIdx ] ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( i != posMinIdx and i != posMaxIdx ) : NEW_LINE INDENT res . append ( arr [ i ] ) NEW_LINE DEDENT DEDENT for i in range ( len ( res ) ) : NEW_LINE INDENT print ( res [ i ] , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT elif ( len ( neg ) >= 2 ) : NEW_LINE INDENT negMax = - sys . maxsize NEW_LINE negMaxIdx = - 1 NEW_LINE negMin = sys . maxsize NEW_LINE negMinIdx = - 1 NEW_LINE for i in range ( len ( neg ) ) : NEW_LINE INDENT if ( abs ( arr [ neg [ i ] ] ) > negMax ) : NEW_LINE INDENT negMaxIdx = neg [ i ] NEW_LINE negMax = abs ( arr [ negMaxIdx ] ) NEW_LINE DEDENT DEDENT for i in range ( len ( neg ) ) : NEW_LINE INDENT if ( abs ( arr [ neg [ i ] ] ) < negMin and neg [ i ] != negMaxIdx ) : NEW_LINE INDENT negMinIdx = neg [ i ] NEW_LINE negMin = abs ( arr [ negMinIdx ] ) NEW_LINE DEDENT DEDENT res . append ( arr [ negMinIdx ] ) NEW_LINE res . append ( arr [ negMaxIdx ] ) NEW_LINE for i in range ( n ) : NEW_LINE INDENT if ( i != negMinIdx and i != negMaxIdx ) : NEW_LINE INDENT res . append ( arr [ i ] ) NEW_LINE DEDENT DEDENT for i in range ( len ( res ) ) : NEW_LINE INDENT print ( res [ i ] , end = " ▁ " ) NEW_LINE DEDENT print ( ) NEW_LINE DEDENT else : NEW_LINE INDENT print ( " No ▁ swap ▁ required " ) NEW_LINE DEDENT DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT arr = [ - 4 , 1 , 6 , - 3 , - 2 , - 1 ] NEW_LINE n = len ( arr ) NEW_LINE getMinimumSum ( arr , n ) NEW_LINE DEDENT