Given an integer array poly[] representing the coefficients of a polynomial in decreasing order of powers of x, and an integer x, evaluate the polynomial at x and return its value. The polynomial represented by poly[] = {a0, a1, a2, ..., an} is: a0xn + a1xn-1 + ... + an-1x + an
Examples:
Input: poly = [2, -6, 2, -1], x = 3
Output: 5
Explanation: The polynomial is 2x³ − 6x² + 2x − 1. Evaluating it at x = 3 gives 2(3³) − 6(3²) + 2(3) − 1 = 54 − 54 + 6 − 1 = 5.Input: poly = [1, 2, 0, 4] x = 2
Output: 20
Explanation: The polynomial is x³ + 2x² + 4. Evaluating it at x = 2 gives 2³ + 2(2²) + 4 = 8 + 8 + 4 = 20.
Table of Content
[Naive Approach] Using Direct Power Calculation - O(n ^ 2) Time and O(1) Space
The idea is to evaluate each term of the polynomial separately by calculating the required power of x. We multiply each coefficient with its corresponding power of x and add all the terms to get the final result.
Working of Approach:
- Traverse the coefficient array from left to right.
- For each coefficient, find its corresponding power of x.
- Calculate the power using repeated multiplication.
- Multiply the coefficient with this power and add it to res.
#include <iostream>
#include <vector>
using namespace std;
int evaluatePoly(vector<int> &poly, int x)
{
int n = poly.size();
int res = 0;
// Evaluate each term of the polynomial.
for (int i = 0; i < n; i++)
{
int power = n - 1 - i;
int curr = 1;
// Calculate x^power.
for (int j = 0; j < power; j++)
{
curr *= x;
}
// Add the current term to the result.
res += poly[i] * curr;
}
// Return the polynomial value.
return res;
}
int main()
{
vector<int> poly = {1, 2, 0, 4};
int x = 2;
cout << evaluatePoly(poly, x);
return 0;
}
import java.util.*;
class GFG {
static int evaluatePoly(int[] poly, int x)
{
int n = poly.length;
int res = 0;
// Evaluate each term of the polynomial.
for (int i = 0; i < n; i++) {
int power = n - 1 - i;
int curr = 1;
// Calculate x^power.
for (int j = 0; j < power; j++) {
curr *= x;
}
// Add the current term to the result.
res += poly[i] * curr;
}
// Return the polynomial value.
return res;
}
public static void main(String[] args)
{
int[] poly = { 1, 2, 0, 4 };
int x = 2;
System.out.println(evaluatePoly(poly, x));
}
}
def evaluatePoly(poly, x):
n = len(poly)
res = 0
# Evaluate each term of the polynomial.
for i in range(n):
power = n - 1 - i
curr = 1
# Calculate x^power.
for j in range(power):
curr *= x
# Add the current term to the result.
res += poly[i] * curr
# Return the polynomial value.
return res
if __name__ == "__main__":
poly = [1, 2, 0, 4]
x = 2
print(evaluatePoly(poly, x))
using System;
class GFG {
public int evaluatePoly(int[] poly, int x)
{
int n = poly.Length;
int res = 0;
// Evaluate each term of the polynomial.
for (int i = 0; i < n; i++) {
int power = n - 1 - i;
int curr = 1;
// Calculate x^power.
for (int j = 0; j < power; j++) {
curr *= x;
}
// Add the current term to the result.
res += poly[i] * curr;
}
// Return the polynomial value.
return res;
}
public static int Main()
{
int[] poly = { 1, 2, 0, 4 };
int x = 2;
GFG obj = new GFG();
Console.WriteLine(obj.evaluatePoly(poly, x));
return 0;
}
}
function evaluatePoly(poly, x)
{
let n = poly.length;
let res = 0;
// Evaluate each term of the polynomial.
for (let i = 0; i < n; i++) {
let power = n - 1 - i;
let curr = 1;
// Calculate x^power.
for (let j = 0; j < power; j++) {
curr *= x;
}
// Add the current term to the result.
res += poly[i] * curr;
}
// Return the polynomial value.
return res;
}
// Driver Code
let poly = [ 1, 2, 0, 4 ];
let x = 2;
console.log(evaluatePoly(poly, x));
Output
20
[Expected Approach] Using Horner's Rule - O(n) Time and O(1) Space
The idea is to rewrite the polynomial in a nested form using Horner's Rule. Instead of calculating powers of x separately, we repeatedly multiply the current result by x and add the next coefficient.
Why does this approach work?
Horner's Rule works by repeatedly factoring out x from the polynomial and representing it in a nested form. This allows the polynomial to be evaluated using only one multiplication and one addition for each coefficient.
For example, the polynomial: x³ + 2x² + 0x + 4, can be written as: ((1 × x + 2) × x + 0) × x + 4
Working of Approach:
- Start with res = 0.
- Traverse all coefficients from left to right.
- Multiply res by x and add the current coefficient.
- This automatically accounts for all required powers of x.
Let us understand with an example:
Input: poly = [1, 2, 0, 4], x = 2
- Initially, res = 0.
- For coeff = 1: res = 0 × 2 + 1 = 1.
- For coeff = 2: res = 1 × 2 + 2 = 4.
- For coeff = 0: res = 4 × 2 + 0 = 8.
- For coeff = 4: res = 8 × 2 + 4 = 20.
Hence, the polynomial value is 20.
#include <iostream>
#include <vector>
using namespace std;
int evaluatePoly(vector<int> &poly, int x)
{
int res = 0;
// Evaluate the polynomial using Horner's Rule.
for (int coeff : poly)
{
res = res * x + coeff;
}
// Return the computed polynomial value.
return res;
}
int main()
{
vector<int> poly = {1, 2, 0, 4};
int x = 2;
cout << evaluatePoly(poly, x);
return 0;
}
import java.util.*;
class GFG {
static int evaluatePoly(int[] poly, int x)
{
int res = 0;
// Evaluate the polynomial using Horner's Rule.
for (int coeff : poly) {
res = res * x + coeff;
}
// Return the computed polynomial value.
return res;
}
public static void main(String[] args)
{
int[] poly = { 1, 2, 0, 4 };
int x = 2;
System.out.println(evaluatePoly(poly, x));
}
}
def evaluatePoly(poly, x):
res = 0
# Evaluate the polynomial using Horner's Rule.
for coeff in poly:
res = res * x + coeff
# Return the computed polynomial value.
return res
if __name__ == "__main__":
poly = [1, 2, 0, 4]
x = 2
print(evaluatePoly(poly, x))
using System;
class GFG {
public int evaluatePoly(int[] poly, int x)
{
int res = 0;
// Evaluate the polynomial using Horner's Rule.
foreach(int coeff in poly)
{
res = res * x + coeff;
}
// Return the computed polynomial value.
return res;
}
public static int Main()
{
int[] poly = { 1, 2, 0, 4 };
int x = 2;
GFG obj = new GFG();
Console.WriteLine(obj.evaluatePoly(poly, x));
return 0;
}
}
function evaluatePoly(poly, x)
{
let res = 0;
// Evaluate the polynomial using Horner's Rule.
for (let coeff of poly) {
res = res * x + coeff;
}
// Return the computed polynomial value.
return res;
}
// Driver Code
let poly = [ 1, 2, 0, 4 ];
let x = 2;
console.log(evaluatePoly(poly, x));
Output
20