Minimize the sum of roots of a given polynomial

Given an array whose elements represents the coefficients of a polynomial of degree n, if the polynomial has a degree n then the array will have n+1 elements (one extra for the constant of a polynomial). Swap some elements of the array and print the resulting array such that the sum of the roots of the given polynomial is as minimum as possible irrespective of the nature of the roots.

Note that : except the first element of the array elements can be 0 also and the degree of the polynomial is always greater than 1.

Examples:

Input : -4 1 6 -3 -2 -1
Output : 1 6 -4 -3 -2 -1
        Here, the array is -4, 1, 6, -3, -2, -1
        i.e the polynomial is -4.x^5 + 1.x^4 + 6.x^3 - 3.x^2 - 2.x^1 - 1
        minimum sum = -6 

Input : -9 0 9
Output :-9 0 9
       Here polynomial is 
       -9.x^2 + 0.x^1 + 9
       minimum sum = 0

Solution : Let us recall the fact about the sum of the roots of a polynomial if a polynomial p(x) = a.x^n + b.x^n-1 + c.x^n-2 + … + k, then the sum of roots of a polynomial is given by -b/a. Please see Vieta’s formulas for details.

We have to minimize -b/a i.e to maximize b/a i.e maximize b and minimize a. So if somehow we are able to maximize b and minimize a, we will swap the values of the coefficients and copy rest of the array as it is.

There will be four cases :
Case #1: when the number of positive coefficients and the number of negative coefficients both are greater than or equal to 2
In this case, we will find a maximum and minimum from positive elements and from negative elements also and we will check -(maxPos)/(minPos) is smaller or -( abs(maxNeg) )/ ( abs(minNeg) ) is smaller and print the answer after swapping accordingly.

Case #2: when the number of positive coefficients is greater than equal to 2 but the number of negative coefficients is less than 2
In this case, we will consider the case of the maximum of positive and minimum of positive elements only. Because if we picked up one from positive elements and the other from negative elements the result of -b/a will be a positive value which is not minimum. (as we require a large negative value)

Case #3: when the number of negative coefficients is greater than equal to 2 but the number of positive coefficients is less than 2
In this case, we will consider the case of the maximum of negative and minimum of negative elements only. Because if we picked up one from positive elements and the other from negative elements the result of -b/a will be a positive value which is not minimum. (as we require a large negative value)

Case #4: When both the counts are less than or equal to 1
Observe carefully, You cannot swap elements in this case.

filter_none

edit
close

play_arrow

link
brightness_4
code

// CPP program to find minimum sum of roots of a 
// given polynomial 
#include <bits/stdc++.h>
using namespace std;
  
void getMinimumSum(int arr[], int n)
{
    // resultant vector
    vector<int> res;
  
    // a vector that store indices of the positive
    // elements
    vector<int> pos;
  
    // a vector that store indices of the negative
    // elements
    vector<int> neg;
  
    for (int i = 0; i < n; i++) {
        if (arr[i] > 0) 
            pos.push_back(i);        
        else if (arr[i] < 0) 
            neg.push_back(i);        
    }
  
    // Case - 1:
    if (pos.size() >= 2 && neg.size() >= 2) {
        int posMax = INT_MIN, posMaxIdx = -1;
        int posMin = INT_MAX, posMinIdx = -1;
  
        int negMax = INT_MIN, negMaxIdx = -1;
        int negMin = INT_MAX, negMinIdx = -1;
  
        for (int i = 0; i < pos.size(); i++) {
            if (arr[pos[i]] > posMax) {
                posMaxIdx = pos[i];
                posMax = arr[posMaxIdx];
            }
        }
  
        for (int i = 0; i < pos.size(); i++) {
            if (arr[pos[i]] < posMin && 
                pos[i] != posMaxIdx) {
                posMinIdx = pos[i];
                posMin = arr[posMinIdx];
            }
        }
  
        for (int i = 0; i < neg.size(); i++) {
            if (abs(arr[neg[i]]) > negMax) {
                negMaxIdx = neg[i];
                negMax = abs(arr[negMaxIdx]);
            }
        }
  
        for (int i = 0; i < neg.size(); i++) {
            if (abs(arr[neg[i]]) < negMin && 
                neg[i] != negMaxIdx) {
                negMinIdx = neg[i];
                negMin = abs(arr[negMinIdx]);
            }
        }
  
        double posVal = -1.0 * (double)posMax / (double)posMin;
        double negVal = -1.0 * (double)negMax / (double)negMin;
  
        if (posVal < negVal) {
            res.push_back(arr[posMinIdx]);
            res.push_back(arr[posMaxIdx]);
  
            for (int i = 0; i < n; i++) {
                if (i != posMinIdx && i != posMaxIdx) {
                    res.push_back(arr[i]);
                }
            }
        }
        else {
            res.push_back(arr[negMinIdx]);
            res.push_back(arr[negMaxIdx]);
  
            for (int i = 0; i < n; i++) {
                if (i != negMinIdx && i != negMaxIdx) {
                    res.push_back(arr[i]);
                }
            }
        }
        for (int i = 0; i < res.size(); i++) {
            cout << res[i] << " ";
        }
        cout << "\n";
    }
  
    // Case - 2:
    else if (pos.size() >= 2) {
        int posMax = INT_MIN, posMaxIdx = -1;
        int posMin = INT_MAX, posMinIdx = -1;
  
        for (int i = 0; i < pos.size(); i++) {
            if (arr[pos[i]] > posMax) {
                posMaxIdx = pos[i];
                posMax = arr[posMaxIdx];
            }
        }
  
        for (int i = 0; i < pos.size(); i++) {
            if (arr[pos[i]] < posMin &&
                pos[i] != posMaxIdx) {
                posMinIdx = pos[i];
                posMin = arr[posMinIdx];
            }
        }
  
        res.push_back(arr[posMinIdx]);
        res.push_back(arr[posMaxIdx]);
  
        for (int i = 0; i < n; i++) {
            if (i != posMinIdx && i != posMaxIdx) {
                res.push_back(arr[i]);
            }
        }
        for (int i = 0; i < res.size(); i++) {
            cout << res[i] << " ";
        }
        cout << "\n";
    }
  
    // Case - 3:
    else if (neg.size() >= 2) {
        int negMax = INT_MIN, negMaxIdx = -1;
        int negMin = INT_MAX, negMinIdx = -1;
  
        for (int i = 0; i < neg.size(); i++) {
            if (abs(arr[neg[i]]) > negMax) {
                negMaxIdx = neg[i];
                negMax = abs(arr[negMaxIdx]);
            }
        }
  
        for (int i = 0; i < neg.size(); i++) {
            if (abs(arr[neg[i]]) < negMin && 
                neg[i] != negMaxIdx) {
                negMinIdx = neg[i];
                negMin = abs(arr[negMinIdx]);
            }
        }
  
        res.push_back(arr[negMinIdx]);
        res.push_back(arr[negMaxIdx]);
  
        for (int i = 0; i < n; i++) 
            if (i != negMinIdx && i != negMaxIdx) 
                res.push_back(arr[i]);
  
        for (int i = 0; i < res.size(); i++) 
            cout << res[i] << " ";
          
        cout << "\n";
    }
  
    // Case - 4:
    else {
        cout << "No swap required\n";
    }
}
  
int main()
{
    int arr[] = { -4, 1, 6, -3, -2, -1 };
    int n = sizeof(arr) / sizeof(arr[0]);
    getMinimumSum(arr, n);
    return 0;
}

chevron_right


Output:

1 6 -4 -3 -2 -1


My Personal Notes arrow_drop_up

Always code as if the guy who ends up maintaining your code will be violent psychopath who knows where you live visit wwwsahilkhoslacoin

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.