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 email@example.com. 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
- Horner's Method for Polynomial Evaluation
- Complete the sequence generated by a polynomial
- Finding nth term of any Polynomial Sequence
- Check if the sum of digits of N is palindrome
- Count of numbers whose sum of increasing powers of digits is equal to the number itself
- Print all perfect squares from the given range
- Maximum sum of two elements whose digit sum is equal
- Find the value of N XOR'ed to itself K times
- Game Theory (Normal-form Game) | Set 4 (Dominance Property-Pure Strategy)
- Goldbach's Weak Conjecture for Odd numbers
- Game Theory (Normal-form Game) | Set 5 (Dominance Property-Mixed Strategy)
- Find all the possible remainders when N is divided by all positive integers from 1 to N+1