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.
- Minimize the sum of roots of a given polynomial
- Program for Derivative of a Polynomial
- Complete the sequence generated by a polynomial
- Horner's Method for Polynomial Evaluation
- Finding nth term of any Polynomial Sequence
- Count of integers in a range which have even number of odd digits and odd number of even digits
- Print all the permutation of length L using the elements of an array | Iterative
- Sum of N terms in the expansion of Arcsin(x)
- Minimize the cost of buying the Objects
- Count of all possible pairs of disjoint subsets of integers from 1 to N
- Right most non-zero digit in multiplication of array elements
- Find the remainder when First digit of a number is divided by its Last digit
- Find the remaining balance after the transaction
- Count of integers that divide all the elements of the given array