Polynomials such as 5 x 4 + 2 x 3 + 7 x 2 + 10 x − 8 5x^4 + 2x^3 + 7x^2 + 10x - 8 5 x 4 + 2 x 3 + 7 x 2 + 10 x − 8 appear in scientific computing, graphics and error-correcting codes. To add or multiply them with a program, we first need a way to store them. This page compares two array representations and gives an efficient addition algorithm.
Two ways to store a polynomial
1. Coefficient array (dense form). Use an array c where c[e] is the coefficient of x e x^e x e . A polynomial of degree n n n needs n + 1 n + 1 n + 1 cells.
5x^4 + 2x^3 + 7x^2 + 10x - 8 as a coefficient array Copy index (exponent): 0 1 2 3 4
coefficient: -8 10 7 2 5
This is simple and fast for polynomials with few zero coefficients, but wasteful for something like x 1000 + 1 x^{1000} + 1 x 1000 + 1 , which would need 1001 cells for 2 terms.
2. Array of terms (sparse form). Store only the non-zero terms as (coefficient, exponent) pairs, kept in decreasing order of exponent . A structure fits naturally:
A term structure Copy typedef struct { int coef; int exp; } Term;
Term p[] = {{5, 4}, {2, 3}, {7, 2}, {10, 1}, {-8, 0}};
Definition 2.13 · Sparse representation of a polynomial
In the term representation , a polynomial is an array of (coefficient, exponent) pairs for its non-zero terms only, sorted so that each exponent is smaller than the one before.
Coefficient array Array of terms Cells needed degree + 1 2 × (number of non-zero terms) Good for Dense polynomials (few zero coefficients) Sparse polynomials (x 1000 + 1 x^{1000} + 1 x 1000 + 1 ) Find coefficient of x e x^e x e O ( 1 ) O(1) O ( 1 ) : c[e]Search the terms Addition Add element by element Merge the two sorted lists
Adding two polynomials
Intuition
Two queues of people sorted by height, tallest first, must merge into one sorted queue. Compare the two people at the front: the taller one steps forward. If both are exactly the same height, they combine into one. When one queue is empty, the rest of the other simply walks in. Polynomial terms merge in the same way by exponent.
Algorithm for C = A + B C = A + B C = A + B (terms of A A A and B B B sorted by decreasing exponent):
Look at the current term of A A A and of B B B .
If A A A ‘s exponent is larger, copy A A A ‘s term to C C C and move on in A A A ; if B B B ‘s is larger, do the same with B B B .
If the exponents are equal, add the coefficients; copy the sum to C C C only if it is not zero; move on in both.
When one polynomial is finished, copy the remaining terms of the other.
Each step moves forward in at least one of the polynomials, so the time is O ( m + n ) O(m + n) O ( m + n ) for m m m and n n n terms, and the result is already sorted.
Example 2.14 Add two polynomials with the merge algorithm
Goal. Add A = x 8 + 5 x 7 + 3 x 6 + 2 x 4 + x 2 A = x^8 + 5x^7 + 3x^6 + 2x^4 + x^2 A = x 8 + 5 x 7 + 3 x 6 + 2 x 4 + x 2 and B = 6 x 9 + 5 x 7 + 3 x 6 + 5 x 2 B = 6x^9 + 5x^7 + 3x^6 + 5x^2 B = 6 x 9 + 5 x 7 + 3 x 6 + 5 x 2 , and also two polynomials whose x 3 x^3 x 3 terms cancel.
By hand. x 9 x^9 x 9 : only in B B B → 6 x 9 6x^9 6 x 9 . x 8 x^8 x 8 : only in A A A → x 8 x^8 x 8 . x 7 x^7 x 7 : 5 + 5 = 10 5 + 5 = 10 5 + 5 = 10 . x 6 x^6 x 6 : 3 + 3 = 6 3 + 3 = 6 3 + 3 = 6 . x 4 x^4 x 4 : only in A A A → 2 x 4 2x^4 2 x 4 . x 2 x^2 x 2 : 1 + 5 = 6 1 + 5 = 6 1 + 5 = 6 . So A + B = 6 x 9 + x 8 + 10 x 7 + 6 x 6 + 2 x 4 + 6 x 2 A + B = 6x^9 + x^8 + 10x^7 + 6x^6 + 2x^4 + 6x^2 A + B = 6 x 9 + x 8 + 10 x 7 + 6 x 6 + 2 x 4 + 6 x 2 .
Program. The terms are structures; the function returns the number of terms in the sum.
C · poly_add.c Copy #include <stdio.h>
#define MAX_TERMS 20
typedef struct {
int coef;
int exp;
} Term;
/* Add polynomials a (na terms) and b (nb terms). Both are sorted by
exponent in decreasing order. The sum goes into c, also sorted.
Returns the number of terms in c. Time: O(na + nb). */
int poly_add( const Term a [] , int na, const Term b [] , int nb, Term c [] )
{
int i = 0 , j = 0 , k = 0 ;
while (i < na && j < nb) {
if (a[i].exp > b[j].exp)
c[k ++ ] = a[i ++ ]; /* only a has this power */
else if (a[i].exp < b[j].exp)
c[k ++ ] = b[j ++ ]; /* only b has this power */
else { /* same power: add */
int sum = a[i].coef + b[j].coef;
if (sum != 0 ) { /* drop terms that cancel */
c[k].coef = sum;
c[k].exp = a[i].exp;
k ++ ;
}
i ++ ;
j ++ ;
}
}
while (i < na) /* copy what is left */
c[k ++ ] = a[i ++ ];
while (j < nb)
c[k ++ ] = b[j ++ ];
return k;
}
void print_poly( const char * label, const Term p [] , int n)
{
printf( "%s" , label);
if (n == 0 )
printf( "0" );
for ( int i = 0 ; i < n; i ++ ) {
int c = p[i].coef;
if (i > 0 )
printf(c < 0 ? " - " : " + " );
else if (c < 0 )
printf( "-" );
if (c < 0 )
c = - c;
if (p[i].exp == 0 )
printf( "%d" , c);
else if (p[i].exp == 1 )
printf( "%dx" , c);
else
printf( "%dx^%d" , c, p[i].exp);
}
printf( " \n " );
}
int main( void )
{
Term a [] = {{ 1 , 8 }, { 5 , 7 }, { 3 , 6 }, { 2 , 4 }, { 1 , 2 }};
Term b [] = {{ 6 , 9 }, { 5 , 7 }, { 3 , 6 }, { 5 , 2 }};
Term p [] = {{ 4 , 3 }, { - 2 , 1 }, { 7 , 0 }};
Term q [] = {{ - 4 , 3 }, { 3 , 2 }, { 2 , 1 }};
Term c[MAX_TERMS];
int n;
print_poly( "A = " , a, 5 );
print_poly( "B = " , b, 4 );
n = poly_add(a, 5 , b, 4 , c);
print_poly( "A + B = " , c, n);
print_poly( "P = " , p, 3 );
print_poly( "Q = " , q, 3 );
n = poly_add(p, 3 , q, 3 , c);
print_poly( "P + Q = " , c, n);
return 0 ;
}
Run it.
Output Copy A = 1x^8 + 5x^7 + 3x^6 + 2x^4 + 1x^2
B = 6x^9 + 5x^7 + 3x^6 + 5x^2
A + B = 6x^9 + 1x^8 + 10x^7 + 6x^6 + 2x^4 + 6x^2
P = 4x^3 - 2x + 7
Q = -4x^3 + 3x^2 + 2x
P + Q = 3x^2 + 7
Result. A + B = 6 x 9 + x 8 + 10 x 7 + 6 x 6 + 2 x 4 + 6 x 2 A + B = 6x^9 + x^8 + 10x^7 + 6x^6 + 2x^4 + 6x^2 A + B = 6 x 9 + x 8 + 10 x 7 + 6 x 6 + 2 x 4 + 6 x 2 , found in O ( m + n ) O(m + n) O ( m + n ) time; the cancelled x 3 x^3 x 3 term of P + Q P + Q P + Q is dropped.
Correction · Program 1.2 (polynomial addition)
Study material says: Program 1.2 (§2.4) uses void main(), compares every term of one polynomial with every term of the other, and prints the result in the order “matched terms, unmatched terms of the first, unmatched terms of the second” (its sample output is 6 2, 6 6, 10 7, 2 4, 1 8, 6 9). It also asks for “less than 6” terms although the arrays hold 6.
Correct version: Use int main(void) and return 0. Keep both polynomials sorted by decreasing exponent (as §2.4 itself assumes) and merge them as shown above: the result is sorted, cancelled terms are removed, and the time is O ( m + n ) O(m + n) O ( m + n ) instead of O ( m ⋅ n ) O(m \cdot n) O ( m ⋅ n ) . The arrays poly1[6][2] allow up to 6 terms (“at most 6”).
Why: C17 §5.1.2.2.1 defines main with return type int. The sample output of Program 1.2 is not in exponent order, and its nested loops make m × n m \times n m × n comparisons. Running the merge program on the same input gives 6 x 9 + x 8 + 10 x 7 + 6 x 6 + 2 x 4 + 6 x 2 6x^9 + x^8 + 10x^7 + 6x^6 + 2x^4 + 6x^2 6 x 9 + x 8 + 10 x 7 + 6 x 6 + 2 x 4 + 6 x 2 , the same terms in correct order.
Key idea
Store a sparse polynomial as an array of (coefficient, exponent) terms in decreasing exponent order; then two polynomials can be added by a single merge pass in O ( m + n ) O(m + n) O ( m + n ) time.
TEE Exam note
For “represent a polynomial using arrays and write an algorithm to add two polynomials”: show both representations with one example, state which suits sparse polynomials, write the four-step merge algorithm (or the C function), trace it on a small example, and state the complexity O ( m + n ) O(m + n) O ( m + n ) . Remember to drop terms whose coefficients add to zero.
Common mistake
Forgetting to copy the remaining terms after one polynomial ends. The loop that compares terms stops as soon as either list is finished; the leftover terms must be copied separately.
Common mistake
Keeping a zero term in the result, e.g. 0 x 3 0x^3 0 x 3 . It is harmless in value but breaks the rule that only non-zero terms are stored.
Key takeaways
Dense form: c[e] = coefficient of x e x^e x e ; needs degree + 1 cells.
Sparse form: array of (coef, exp) structures sorted by decreasing exponent.
Addition by merging takes O ( m + n ) O(m + n) O ( m + n ) and gives a sorted result.
Equal exponents: add coefficients and drop the term if the sum is 0.
Practice questions Answer on paper first, then check. The explanation matters more than the letter you chose.
Question 1 · multiple choice How many cells does the coefficient-array representation of 3 x 50 + 2 3x^{50} + 2 3 x 50 + 2 need?
A 2 B 4 C 50 D 51
Check answer
Show answer Answer: D. 51
Exponents 0 to 50 need 51 cells, although only 2 are non-zero. The term representation needs just 2 pairs (4 numbers).
Question 2 · multiple choice What is the time complexity of adding polynomials with m m m and n n n terms by the merge method?
Check answer
Show answer Answer: B. O ( m + n ) O(m + n) O ( m + n )
Every comparison moves forward in at least one list, so there are at most m + n m + n m + n steps. O ( m ⋅ n ) O(m \cdot n) O ( m ⋅ n ) is the cost of comparing every pair, as Program 1.2 does.
Question 3 · multiple choice A = 4 x 5 + 3 x 2 − x A = 4x^5 + 3x^2 - x A = 4 x 5 + 3 x 2 − x and B = 2 x 4 − 3 x 2 + 6 B = 2x^4 - 3x^2 + 6 B = 2 x 4 − 3 x 2 + 6 . What is A + B A + B A + B ?
Check answer
Show answer Answer: C. 4 x 5 + 2 x 4 − x + 6 4x^5 + 2x^4 - x + 6 4 x 5 + 2 x 4 − x + 6
The x 2 x^2 x 2 terms cancel (3 − 3 = 0 3 - 3 = 0 3 − 3 = 0 ) and are dropped; the other terms are copied in exponent order. Keeping "0 x 2 0x^2 0 x 2 " breaks the non-zero-terms rule.
Question 4 · multiple choice In the merge algorithm, the current terms are ( 7 , 3 ) (7, 3) ( 7 , 3 ) from A A A and ( 2 , 5 ) (2, 5) ( 2 , 5 ) from B B B (coefficient, exponent). What happens next?
Check answer
Show answer Answer: B. ( 2 , 5 ) (2, 5) ( 2 , 5 ) is written to C C C and B B B moves to its next term.
The larger exponent (5) is copied first, because the result must be in decreasing order. Coefficients are added only when the exponents are equal.
Question 5 · multiple choice Why must the terms be sorted by exponent for the merge method?
A Because C cannot store unsorted structures B Because sorted polynomials use less memory C Because unsorted terms would make the coefficients negative D Because only then do equal exponents meet at the fronts of the two lists at the same time
Check answer
Show answer Answer: D. Because only then do equal exponents meet at the fronts of the two lists at the same time
Sorting guarantees that when we pass an exponent in one list, it cannot appear later in that list, so one pass is enough. Memory use is the same either way.
Question 6 · short answer
Write the algorithm to add two polynomials stored as arrays of (coefficient, exponent) terms in decreasing exponent order, and trace it on A = 3 x 4 + 2 x A = 3x^4 + 2x A = 3 x 4 + 2 x and B = 5 x 4 − 2 x + 1 B = 5x^4 - 2x + 1 B = 5 x 4 − 2 x + 1 .
Show worked answer Let i i i , j j j , k k k index A A A , B B B , C C C , all starting at 0. While i < m i < m i < m and j < n j < n j < n : if A [ i ] . e x p > B [ j ] . e x p A[i].exp > B[j].exp A [ i ] . e x p > B [ j ] . e x p , copy A [ i ] A[i] A [ i ] to C [ k ] C[k] C [ k ] and increase i i i , k k k ; else if A [ i ] . e x p < B [ j ] . e x p A[i].exp < B[j].exp A [ i ] . e x p < B [ j ] . e x p , copy B [ j ] B[j] B [ j ] and increase j j j , k k k ; else let s = A [ i ] . c o e f + B [ j ] . c o e f s = A[i].coef + B[j].coef s = A [ i ] . coe f + B [ j ] . coe f , and if s ≠ 0 s \ne 0 s = 0 store ( s , A [ i ] . e x p ) (s, A[i].exp) ( s , A [ i ] . e x p ) in C [ k ] C[k] C [ k ] and increase k k k ; increase i i i and j j j . Then copy any remaining terms of A A A , then of B B B . Trace: ( 3 , 4 ) (3,4) ( 3 , 4 ) vs ( 5 , 4 ) (5,4) ( 5 , 4 ) → equal, sum 8 → C = [ ( 8 , 4 ) ] C = [(8,4)] C = [( 8 , 4 )] . ( 2 , 1 ) (2,1) ( 2 , 1 ) vs ( − 2 , 1 ) (-2,1) ( − 2 , 1 ) → equal, sum 0 → nothing stored. A A A is finished; copy ( 1 , 0 ) (1,0) ( 1 , 0 ) from B B B . Result: 8 x 4 + 1 8x^4 + 1 8 x 4 + 1 . Time O ( m + n ) O(m + n) O ( m + n ) .