Polynomial Addition

PROBLEM :

Given two polynomial numbers represented by a linked list. The task is to complete the  function addPolynomial that adds these lists meaning adds the coefficients who have same variable powers.

Example:

Input:
     1st number = 5x^2 + 4x^1 + 2x^0
     2nd number = 5x^1 + 5x^0
Output:
        5x^2 + 9x^1 + 7x^0
Input:
     1st number = 5x^3 + 4x^2 + 2x^0
     2nd number = 5x^1 + 5x^0
Output:
        5x^3 + 4x^2 + 5x^1 + 7x^0

Input:
The first line of input contains an integer T denoting the no of test cases. Then in the next line is an integer N denoting the no of terms of first polynomial. In the next line are N space separated pairs x and y where x denotes the coefficient and y denotes the power. Similarly In the next line is an integer M denoting the no of terms of the second polynomial and in the line following it are N space separated pairs for second polynomial.

Output:
For each test case in a new line print the required polynomial in decreasing order of the power.

Constraints:
1<=T<=100
1<=N,M<=100
1<=x,y<=1000

Example:
Input:
2
1
1 2
1
1 3
2
1 3 2 2
2
3 3 4 2

Output:
1x^3 + 1x^2
4x^3 + 6x^2

--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------

/* Structure of Node used
struct Node
{
    int coeff;  // coefficient of the polynomial
    int pow;   // power of the polynomial
    Node* next;
};
*/

/* The below method print the required sum of polynomial
p1 and p2 as specified in output  */


void addPolynomial(Node *p1, Node *p2)
{
    if(p1==NULL&&p2==NULL)
        return ;
       
    if(p2==NULL){
        while(p1!=NULL){
            cout<<p1->coeff<<"x^"<<p1->pow ;
            if(p1->next!=NULL)
                cout<<" + " ;
            p1=p1->next ;
        }  
    }
   
    if(p1==NULL){
        while(p2!=NULL){
            cout<<p2->coeff<<"x^"<<p2->pow ;
            if(p2->next!=NULL)
                cout<<" + " ;
            p2=p2->next ;
        }  
    }
   
    while(p1!=NULL&&p2!=NULL){
        if(p1->pow==p2->pow){
            cout<<p1->coeff+p2->coeff<<"x^"<<p1->pow ;
            if(p1->next!=NULL||p2->next!=NULL)
                cout<<" + " ;
            p1=p1->next ;
            p2=p2->next ;
        }
        else if(p1->pow>p2->pow){
            cout<<p1->coeff<<"x^"<<p1->pow ;
            if(p1->next!=NULL||p2!=NULL)
                cout<<" + " ;
            p1=p1->next ;
        }
        else{
            cout<<p2->coeff<<"x^"<<p2->pow ;
            if(p1->next!=NULL||p2!=NULL)
                cout<<" + " ;
            p2=p2->next ;
        }
    }
   
    while(p1!=NULL){
        cout<<p1->coeff<<"x^"<<p1->pow ;
        if(p1->next!=NULL)
            cout<<" + " ;
        p1=p1->next ;
    }
   
    while(p2!=NULL){
        cout<<p2->coeff<<"x^"<<p2->pow ;
        if(p2->next!=NULL)
            cout<<" + " ;
        p2=p2->next ;
    }
}

---------------------------------------------------------------------------------

Comments

Popular posts from this blog

Count ways to N'th Stair(Order does not matter)

Replace all ‘0’ with ‘5’ in an input Integer

Chocolate Distribution Problem

Remove characters from the first string which are present in the second string

Primality Test ( CodeChef Problem code: PRB01 )