Given a polynomial function f(x) = 1+ a1*x + a2*(x^2) + … an(x^n). Find the Sgn value of these function, when x is given and all the coefficients also.
If value of polynomial greater than 0 Sign = 1 Else If value of polynomial less than 0 Sign = -1 Else if value of polynomial is 0 Sign = 0
Input: poly = [1, 2, 3] x = 1 Output: 1 Explanation: f(1) = 6 which is > 0 hence 1. Input: poly = [1, -1, 2, 3] x = -2 Output: -1 Explanation: f(-2)=-11 which is less then 0, hence -1.
A naive approach will be to calculate every power of x and then add it to the answer by multiplying it with its coefficient. Calculating power of x will take O(n) time and for n coefficients. Hence taking the total complexity to O(n * n)
An efficient approach is to use Horner’s method. We evaluate value of polynomial using Horner’s method. Then we return value according to sing of the value.
Below is the implementation of the above approach
Sign of polynomial is 1
Time complexity : O(n)
This article is contributed by Raja Vikramaditya. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Integration in a Polynomial for a given value
- Horner's Method for Polynomial Evaluation
- Complete the sequence generated by a polynomial
- Finding nth term of any Polynomial Sequence
- Python | Finding Solutions of a Polynomial Equation
- Program to find the indefinite Integration of the given Polynomial
- Content of a Polynomial
- String hashing using Polynomial rolling hash function
- Program for Derivative of a Polynomial
- Minimize the sum of roots of a given polynomial
- Program to differentiate the given Polynomial
- Solving Homogeneous Recurrence Equations Using Polynomial Reduction
- Find the maximum possible value of the minimum value of modified array
- Find length of period in decimal value of 1/n
- Find minimum number of coins that make a given value
- Print all Jumping Numbers smaller than or equal to a given value
- Find the minimum value of m that satisfies ax + by = m and all values after m also satisfy
- Trapezoidal Rule for Approximate Value of Definite Integral
- Maximum value in an array after m range increment operations
- C program to calculate the value of nPr