Polynomial Addition
Submissions: 6957   Accuracy:

35.2%

  Difficulty: Medium   Marks: 4
Associated Course(s):   Sudo Placement 2019

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.

Input:
The first line of input contains an integer T denoting the number of test cases. Then in the next line is an integer N denoting the number 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.

User Task:
The task is to complete the function addPolynomial() which should add the polynomial with same powers.

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

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

Explanation:
Testcase 1:
Since, x2 and x3 both have different powers as 2 and 3. So, their coefficient can't be added up.

Testcase 2: Since, x3 has two different coefficients as 3 and 1. Adding them up will lead to 4x3. Also, x2 has two coefficients as 4 and 2. So, adding them up will give 6x2.
 

** For More Input/Output Examples Use 'Expected Output' option **

Contributor: Harshit Sidhwa
Author: Shubham Joshi 1


If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.



Need help with your code? Please use ide.geeksforgeeks.org, generate link and share the link here.


to report an issue on this page.