Multiply two polynomials | Practice | GeeksforGeeks

Given two polynomials represented by two arrays that contains the coefficients of poynomials, returns the polynomial in form of array formed after multiplication of given polynomials.

Example 1:
Input:
M = 4, N = 3
Arr1 = {1 , 0, 3, 2}
Arr2 = {2, 0, 4}
Output: {2, 0, 10, 4, 12, 8}
Explaination: 
First polynomial: 
1 + 0x1 + 3x2 + 2x3
Second polynomial: 
2 + 0x1 + 4x2
Product polynomial:
2 + 0x1 + 10x2 + 4x3 + 12x4 + 8x5

Example 2:
Input:
M = 5, N = 4
Arr1 = {1, 9, 3, 4, 7}
Arr1 = {4, 0, 2, 5}
Output: {4, 36, 14, 39, 79, 23, 34, 35}
Explaination: 
First polynomial: 
1 + 9x1 + 3x2 + 4x3 + 7x4
Second polynomial: 
4 + 0x1 + 2x2 + 5x3
Product polynomial:
4 + 36x1 + 14x2 + 39x3 + 79x4 + 23x5 + 34x6 + 35x7


Your Task:
You don't need to read input or print anything. Your task is to complete the function polyMultiply() which takes the array Arr1[] and Arr2[]and their size M and N as input parameters and returns the polynomial in form of array formed after multiplication of given polynomials..

Expected Time Complexity: O(M*N)
Expected Auxiliary Space: O(M+N)

Constraints:
1 ≤ M, N ≤ 100
1 ≤  Arr1[i] , Arr2[i]  ≤ 100
+1
shivi13verma3 months ago
return numpy.polymul(Arr1,Arr2)
0
harshakarimikonda223 months ago
vector<int> arr;       for(int i=0;i<M+N;i++)       arr.push_back(0);       int index=0;       for(int i=0;i<M;i++){           for(int j=0;j<N;j++){               int mul=Arr1[i]*Arr2[j];               index=i+j;               int sum=arr[index]+mul;               arr[index]=sum;           }       }       return arr;
0
mohankrishnapeddineni3 months ago
class Solution{   public int[] polyMultiply(int Arr1[], int Arr2[], int M, int N)   {       int arr[]= new int[M+N-1];       for(int i=0;i<M;i++)       {           for(int j=0;j<N;j++)           {               arr[i+j]+= Arr1[i]*Arr2[j];           }       }           return arr;   }}
-1
avinav26113 months ago
Easy C++ Solution
 
0
ritikyadavr113 months ago
 vector<int>polyMultiply(int Arr1[], int Arr2[], int M, int N)   {       // code here       vector<int>ans(N+M);       int index = 0;       for(int i=0; i<M; i++){           for(int j=0; j<N; j++){              int  mul = Arr1[i] * Arr2[j];                index = i+j;                int sum = ans[index] + mul;                ans[index] = sum;           }       }       return ans;   }
0
mohitkumar423 months ago
java solution 
 
 
class Solution{   public int[] polyMultiply(int Arr1[], int Arr2[], int M, int N)   {       int arr[] = new int[M+N-1];       int k =0;       for(int i =0;i<M;i++)       {           for(int j =0;j<N;j++)           {   if(i==0)               {arr[k]=Arr1[i]*Arr2[j];               k++;}               else               arr[i+j]= arr[i+j]+ Arr1[i]*Arr2[j];           }       }       return arr;   }}
+1
swapniltayal4224 months ago
import numpy as npclass Solution:def polyMultiply(self, Arr1, Arr2, M, N): return np.polymul(Arr1, Arr2)
 
 
😑😑0.1sec
 
0
mashhadihossain5 months ago
SIMPLE JAVA SOLUTION (0.4 sec)
int value(int ans[][],int k)   {       int sum=0;       for(int i=0;i<ans.length;i++)       {           for(int j=0;j<ans[i].length;j++)           {               if(i+j==k)               {                   sum+=ans[i][j];               }           }       }       return sum;   }   public int[] polyMultiply(int Arr1[], int Arr2[], int M, int N)   {       int ans[][]=new int[M][N];       for(int i=0;i<M;i++)       {           for(int j=0;j<N;j++)           {               ans[i][j]=Arr1[i]*Arr2[j];             }       }       int a[]=new int[M+N-1];       for(int i=0;i<a.length;i++)       {           a[i]=value(ans,i);       }       return a;                  }
0
e20040806 months ago
runtime: 0.1/16.1
import numpy as np
class Solution:
	def polyMultiply(self, Arr1, Arr2, M, N):
		return np.polymul(Arr1, Arr2)
0
deepikaballa137 months ago
for(int i=0;i<M;i++){         for(int j=0;j<N;j++){             arr[i+j]=arr[i+j]+Arr1[i]*Arr2[j];         }     }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.