 Open in App
Not now

# Sgn value of a polynomial

• Last Updated : 13 Jul, 2022

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```

Examples:

```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
than 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), as we will need nested loops for traversing n*n times.
An efficient approach is to use Horner’s method. We evaluate value of polynomial using Horner’s method. Then we return value according to sign of the value.
Below is the implementation of the above approach

## C++

 `// CPP program to find sign value of a ` `// polynomial` `#include ` `using` `namespace` `std;` ` `  `// returns value of polyx(n-1) + polyx(n-2) ` `// + .. + poly[n-1]` `int` `horner(``int` `poly[], ``int` `n, ``int` `x)` `{` `    ``int` `result = poly;  ``// Initialize result` ` `  `    ``// Evaluate value of polynomial` `    ``// using Horner's method` `    ``for` `(``int` `i=1; i 0)` `      ``return` `1;` `   ``else` `if` `(result < 0)` `      ``return` `-1; ` `   ``return` `0;` `}` ` `  `// Driver program to test above function.` `int` `main()` `{` `    ``// Let us evaluate value of 2x3 - 6x2` `    ``// + 2x - 1 for x = 3` `    ``int` `poly[] = {2, -6, 2, -1};` `    ``int` `x = 3;` `    ``int` `n = ``sizeof``(poly)/``sizeof``(poly);` `    ``cout << ``"Sign of polynomial is "` `         ``<< findSign(poly, n, x);` `    ``return` `0;` `}`

## Java

 `// Java program to find sign value of a ` `// polynomial`   `class` `GFG` `{` `    ``// returns value of polyx(n-1) + polyx(n-2) ` `    ``// + .. + poly[n-1]` `    ``static` `int` `horner(``int` `poly[], ``int` `n, ``int` `x)` `    ``{` `        ``// Initialize result` `        ``int` `result = poly[``0``]; ` `    `  `        ``// Evaluate value of polynomial` `        ``// using Horner's method` `        ``for` `(``int` `i = ``1``; i < n; i++)` `            ``result = result * x + poly[i];` `    `  `        ``return` `result;` `    ``}` `    `  `    ``// Returns sign value of polynomial` `    ``static` `int` `findSign(``int` `poly[], ``int` `n, ``int` `x)` `    ``{` `        ``int` `result = horner(poly, n, x);` `        ``if` `(result > ``0``)` `            ``return` `1``;` `        ``else` `if` `(result < ``0``)` `            ``return` `-``1``; ` `        ``return` `0``;` `    ``}` `    `  `    ``// Driver code` `    ``public` `static` `void` `main (String[] args) ` `    ``{` `        ``// Let us evaluate value of 2x3 - 6x2` `        ``// + 2x - 1 for x = 3` `        ``int` `poly[] = {``2``, -``6``, ``2``, -``1``};` `        ``int` `x = ``3``;` `        ``int` `n = poly.length;` `        ``System.out.print(``"Sign of polynomial is "``+` `                              ``findSign(poly, n, x));` `    ``}` `}`   `// This code is contributed by Anant Agarwal.`

## Python3

 `# Python3 program to find ` `# sign value of a ` `# polynomial`   `# returns value of polyx(n-1) + ` `# polyx(n-2) + .. + poly[n-1]` `def` `horner( poly, n, x):` `    `  `    ``# Initialize result` `    ``result ``=` `poly[``0``];` `    `  `    ``# Evaluate value of ` `    ``# polynomial using ` `    ``# Horner's method` `    ``for` `i ``in` `range``(``1``,n):` `        ``result ``=` `(result ``*` `x ``+` `                     ``poly[i]);` `    ``return` `result;`   `# Returns sign value ` `# of polynomial` `def` `findSign(poly, n, x):` `    ``result ``=` `horner(poly, n, x);` `    ``if` `(result > ``0``):` `        ``return` `1``;` `    ``elif` `(result < ``0``):` `        ``return` `-``1``;` `    ``return` `0``;`   `# Driver Code`   `# Let us evaluate value ` `# of 2x3 - 6x2` `# + 2x - 1 for x = 3` `poly ``=` `[``2``, ``-``6``, ``2``, ``-``1``];` `x ``=` `3``;` `n ``=` `len``(poly);`   `print``(``"Sign of polynomial is "``, ` `         ``findSign(poly, n, x));`   `# This code is contributed by mits`

## C#

 `// C# program to find sign value of a ` `// polynomial` `using` `System;`   `class` `GFG {` `    `  `    ``// returns value of polyx(n-1)` `    ``// + polyx(n-2) + .. + poly[n-1]` `    ``static` `int` `horner(``int` `[]poly, ``int` `n, ``int` `x)` `    ``{` `        `  `        ``// Initialize result` `        ``int` `result = poly; ` `    `  `        ``// Evaluate value of polynomial` `        ``// using Horner's method` `        ``for` `(``int` `i = 1; i < n; i++)` `            ``result = result * x + poly[i];` `    `  `        ``return` `result;` `    ``}` `    `  `    ``// Returns sign value of polynomial` `    ``static` `int` `findSign(``int` `[]poly, ``int` `n, ``int` `x)` `    ``{` `        `  `        ``int` `result = horner(poly, n, x);` `        `  `        ``if` `(result > 0)` `            ``return` `1;` `        ``else` `if` `(result < 0)` `            ``return` `-1; ` `            `  `        ``return` `0;` `    ``}` `    `  `    ``// Driver code` `    ``public` `static` `void` `Main () ` `    ``{` `        `  `        ``// Let us evaluate value of 2x3 - 6x2` `        ``// + 2x - 1 for x = 3` `        ``int` `[]poly = {2, -6, 2, -1};` `        ``int` `x = 3;` `        ``int` `n = poly.Length;` `        `  `        ``Console.Write(``"Sign of polynomial is "` `                      ``+ findSign(poly, n, x));` `    ``}` `}`   `// This code is contributed by vt_m.`

## PHP

 ` 0)` `        ``return` `1;` `    ``else` `if` `(``\$result` `< 0)` `        ``return` `-1; ` `    ``return` `0;` `}`   `    ``// Driver Code` `    ``// Let us evaluate value ` `    ``// of 2x3 - 6x2` `    ``// + 2x - 1 for x = 3` `    ``\$poly` `= ``array``(2, -6, 2, -1);` `    ``\$x` `= 3;` `    ``\$n` `= ``count``(``\$poly``);` `    ``echo` `"Sign of polynomial is "` `        ``, findSign(``\$poly``, ``\$n``, ``\$x``);`   `// This code is contributed by anuj_67.` `?>`

## Javascript

 ``

Output:

`Sign of polynomial is 1`

Time Complexity: O(N), as we are using a loop to traverse N times.

Auxiliary Space: O(1), as we are not using any extra space.