- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Minimize the sum of roots of a given polynomial in C++
We are given with an array of integer elements representing the coefficient values of a polynomial. The size of an array will be ‘n’ i.e. number of elements in an array. The degree of polynomial always starts with n-1 as there will be one constant value at the end of the polynomial series. The task is to replace the coefficient with other polynomials in such a manner that sum of roots will be minimized.
Let us see various input output scenarios for this -
In − int arr[] = { 2, -1, 4, 9, -1, 10, -5}
Out − Minimize the sum of roots of a given polynomial is: -1 -5 2 4 9 -1 10
Explanation − we are given with an integer array containing 7 elements i.e. polynomial power will be 6. So, we will achieve the minimum output as-: -1 * x^6 - 5 * x^5 + 2 * x^4 + 4 * x^3 + 9 * x^2 - 1 * x^1 + 10 which will result in a minimum sum of roots as -5 and 1.
In − int arr[] = {3, -2, -1, 4}
Out − Minimize the sum of roots of a given polynomial is: -1 -2 3 4
Explanation − we are given with an integer array containing 7 elements i.e. polynomial power will be 6. So, we will achieve the minimum output as-: -1 * x^3 - 2 * x^2 + 3 * x^1 + 4 which will result in a minimum sum of roots as -1.
Approach used in the below program is as follows
Input an array of integer elements and calculate the size of an array. Pass the data to the function for further processing.
Inside the function Minimize_root(arr, size)
Declare variables of type vector of integer type as vec_1, vec_2, vec_3.
Start loop FOR from i to 0 till the size of an array. Inside the loop, check IF arr[i] > 0 then push_back i to vec_2. Else IF, arr[i] < 0 then puch_back i to vec_3.
Calculate the size of vec_2 and vec_3.
Check IF vec_2_size >= 2 AND IF vec_3_size>= 2 then start loop FOR from i to 0 till the size of vec_2 and inside the loop, check IFarr[vec_2[i]] greater than max_val then set temp to vec_2[i] and max_val to arr[temp].
Start loop FOR from i to 0 till the size of a vec_2. Inside the loop, check IF arr[vec_2[i]] less than min_val than check IF vec_2[i] not equals to temp then set temp_2 to vec_2[i] and min_val to arr[temp_2]
Start loop FOR from i to 0 till the size of a vec_3. Inside the loop, check IF abs(arr[vec_3[i]]) greater than N_max then set N_temp to vec_3[i] and N_max to abs(arr[N_temp])
Start loop FOR from i to 0 till vec_3 size and check IF abs(arr[vec_3[i]]) less than N_min than check IFvec_3[i] not equals to N_temp then set N_temp_2 to vec_3[i] and N_min = abs(arr[N_temp_2])
Check IF vec_2_data less than vec_3_data then inside the FOR loop from i to 0 till the size of an array, check IF i not equals to temp_2 AND i not equals to temp then push_back arr[i] to vec_1. ELSE, inside the FOR loop from i to 0 till the size of an array, check IF i not equals to N_temp_2 AND i not equals to N_temp then push_back arr[i] to vec_1.
Start loop FOR to traverse the vec_1 and keep printing the vec_1[i] as a result.
ELSE IF, check vec_2_size >= 2 then start loop FOR from i to 0 till the size of a vec_2. Check IF arr[vec_2[i]] greater than max_val then set temp to vec_2[i] and max_val to arr[temp].
Start loop FOR from i to 0 till i less than size of a vec_2. Inside the loop, check IF arr[vec_2[i]] less than min_val than check IFvec_2[i] not equals to temp than set temp_2 to vec_2[i] and min_val to arr[temp_2].
Start loop FOR from i to 0 till size of an array. Inside the loop, check IF i not equals to temp_2 then check IF i not equals to temp then puch_back arr[i] to vec_1.
ELSE IF, vec_3 >= 2 then start loop FOR from i to 0 till vec_3 size and check IF abs(arr[vec_3[i]]) greater than N_max then set temp to vec_3[i] and N_max to abs(arr[temp]).
Start loop FOR from i to 0 till i less tha vec_3.size() and check IF abs(arr[vec_3[i]]) less than N_min than check IF vec_3[i] not equals to temp than set temp_2 to vec_3[i] and N_min to abs(arr[temp_2]).
Start loop FOR from i to 0 till size of an array. Check IF i not equals to temp_2 then check IF i not equals to temp then push_back arr[i] to vc_1 and keep printing.
Example
#include <bits/stdc++.h> using namespace std; void Minimize_root(int arr[], int size){ vector<int> vec_1; vector<int> vec_2; vector<int> vec_3; for (int i = 0; i < size; i++){ if (arr[i] > 0){ vec_2.push_back(i); } else if (arr[i] < 0){ vec_3.push_back(i); } } int vec_2_size = vec_2.size(); int vec_3_size = vec_3.size(); if(vec_2_size >= 2){ if(vec_3_size>= 2){ int max_val = INT_MIN; //max_val int temp = -1; //temp int min_val = INT_MAX; //min_val int temp_2 = -1; //temp_2 int N_max = INT_MIN; // N_max int N_temp = -1; // N_temp int N_min = INT_MAX; //N_min int N_temp_2 = -1; //N_temp_2 for (int i = 0; i < vec_2.size(); i++){ if (arr[vec_2[i]] > max_val){ temp = vec_2[i]; max_val = arr[temp]; } } for (int i = 0; i < vec_2.size(); i++){ if (arr[vec_2[i]] < min_val){ if(vec_2[i] != temp){ temp_2 = vec_2[i]; min_val = arr[temp_2]; } } } for (int i = 0; i < vec_3.size(); i++){ if (abs(arr[vec_3[i]]) > N_max){ N_temp = vec_3[i]; N_max = abs(arr[N_temp]); } } for (int i = 0; i < vec_3.size(); i++){ if(abs(arr[vec_3[i]]) < N_min ){ if(vec_3[i] != N_temp){ N_temp_2 = vec_3[i]; N_min = abs(arr[N_temp_2]); } } } double vec_2_data = -1.0 * (double)max_val / (double)min_val; double vec_3_data = -1.0 * (double)N_max / (double)N_min; if (vec_2_data < vec_3_data){ vec_1.push_back(arr[temp_2]); vec_1.push_back(arr[temp]); for (int i = 0; i < size; i++){ if (i != temp_2 && i != temp){ vec_1.push_back(arr[i]); } } } else{ vec_1.push_back(arr[N_temp_2]); vec_1.push_back(arr[N_temp]); for (int i = 0; i < size; i++){ if (i != N_temp_2 && i != N_temp){ vec_1.push_back(arr[i]); } } } for (int i = 0; i < vec_1.size(); i++){ cout << vec_1[i] << " "; } } } else if(vec_2_size >= 2){ int max_val = INT_MIN; int temp = -1; int min_val = INT_MAX; int temp_2 = -1; for (int i = 0; i < vec_2.size(); i++){ if (arr[vec_2[i]] > max_val){ temp = vec_2[i]; max_val = arr[temp]; } } for (int i = 0; i < vec_2.size(); i++){ if (arr[vec_2[i]] < min_val){ if(vec_2[i] != temp){ temp_2 = vec_2[i]; min_val = arr[temp_2]; } } } vec_1.push_back(arr[temp_2]); vec_1.push_back(arr[temp]); int i = 0; i < size; i++; { if(i != temp_2){ if(i != temp){ vec_1.push_back(arr[i]); } } } for (int i = 0; i < vec_1.size(); i++){ cout << vec_1[i] << " "; } } else if(vec_3_size >= 2){ int N_max = INT_MIN; int temp = -1; int N_min = INT_MAX; int temp_2 = -1; for (int i = 0; i < vec_3.size(); i++){ if (abs(arr[vec_3[i]]) > N_max){ temp = vec_3[i]; N_max = abs(arr[temp]); } } for (int i = 0; i < vec_3.size(); i++){ if(abs(arr[vec_3[i]]) < N_min){ if(vec_3[i] != temp){ temp_2 = vec_3[i]; N_min = abs(arr[temp_2]); } } } vec_1.push_back(arr[temp_2]); vec_1.push_back(arr[temp]); for (int i = 0; i < size; i++){ if (i != temp_2){ if(i != temp){ vec_1.push_back(arr[i]); } } } for (int i = 0; i < vec_1.size(); i++){ cout << vec_1[i] << " "; } } else { cout<<"Not Possible"; } } int main(){ int arr[] = { 2, -1, 4, 9, -1, 10, -5}; int size = sizeof(arr) / sizeof(arr[0]); cout<<"Minimize the sum of roots of a given polynomial is: "; Minimize_root(arr, size); return 0; }
Output
If we run the above code it will generate the following Output
Minimize the sum of roots of a given polynomial is: -1 -5 2 4 9 -1 10