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.
- Program for Derivative of a Polynomial
- Minimize the sum of roots of a given polynomial
- Complete the sequence generated by a polynomial
- Horner's Method for Polynomial Evaluation
- Finding nth term of any Polynomial Sequence
- Sum of numbers in a range [L, R] whose count of divisors is prime
- Find a pair (n,r) in an integer array such that value of nPr is maximum
- Sum of values of all possible non-empty subsets of the given array
- Find a pair (n,r) in an integer array such that value of nCr is maximum
- Minimum length String with Sum of the alphabetical values of the characters equal to N
- Printing the Triangle Pattern using last term N
- Minimize the cost of partitioning an array into K groups
- Find next greater element with no consecutive 1 in it's binary representation
- Number of ways in which N can be represented as the sum of two positive integers