{"id":"5b809e65-ca24-4020-82da-3bce88476328","name":"Matrix Multiplication","description":"<p>1. You are given a number n1, representing the number of rows of 1st matrix.</p><p>2. You are given a number m1, representing the number of columns of 1st matrix.</p><p>3. You are given n1*m1 numbers, representing elements of 2d array a1.</p><p>4. You are given a number n2, representing the number of rows of 2nd matrix.</p><p>5. You are given a number m2, representing the number of columns of 2nd matrix.</p><p>6. You are given n2*m2 numbers, representing elements of 2d array a2.</p><p>7. If the two arrays representing two matrices of dimensions n1 * m1 and n2 * m2 can be multiplied, display the contents of product array as specified in output format.</p><p>8. If the two arrays can't be multiplied, print \"Invalid input\".</p>","inputFormat":"<p>A number n1</p><p>A number m1</p><p>e11 e12.. e21 e22.. .. n1 * m1 number of elements of array a1</p><p>A number n2</p><p>A number m2</p><p>e11' e12'.. e21' e22'.. .. n2 * m2 number of elements of array a2</p>","outputFormat":"<p>e11'' e12'' e13'' .. e21'' e22'' e23'' .. .. elements of prd array</p>","constraints":"<p>1 &lt;= n1 &lt;= 10^2</p><p>1 &lt;= m1 &lt;= 10^2</p><p>-10^9 &lt;= e11, e12, .. n1 * m1 elements &lt;= 10^9</p><p>1 &lt;= n2 &lt;= 10^2</p><p>1 &lt;= m2 &lt;= 10^2</p><p>-10^9 &lt;= e11', e12', .. n2 * m2 elements &lt;= 10^9</p>","sampleCode":{"cpp":{"code":"#include<iostream>\r\n#include<vector>\r\n\r\nusing namespace std;\r\n\r\nvoid matrixMult(vector<vector<int>> A, vector<vector<int>> B){\r\n// Write your code here.\r\n}\r\n\r\nint main(){\r\n int r1;\r\n int c1;\r\n cin>>r1;\r\n cin>>c1;\r\n\r\n vector<vector<int>> mat1;\r\n for(int i= 0; i< r1; i++){\r\n vector<int> arr;\r\n for(int j= 0; j< c1; j++){\r\n int ele;\r\n cin>> ele;\r\n arr.push_back(ele);\r\n }\r\n mat1.push_back(arr);\r\n }\r\n\r\n int r2;\r\n int c2;\r\n cin>>r2;\r\n cin>>c2;\r\n\r\n vector<vector<int>> mat2;\r\n for(int i= 0; i< r2; i++){\r\n vector<int> arr;\r\n for(int j= 0; j< c2; j++){\r\n int ele;\r\n cin>> ele;\r\n arr.push_back(ele);\r\n }\r\n mat2.push_back(arr);\r\n }\r\n\r\n matrixMult(mat1, mat2);\r\n}"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main{\r\n\r\npublic static void main(String[] args) throws Exception {\r\n // write your code here\r\n }\r\n\r\n}"},"python":{"code":"def main():\r\n\r\nif __name__ == \"__main__\":\r\n main()def matrixMult(mat1, mat2):\r\n# Write your code here. \r\n \r\nr1= int(input())\r\nc1= int(input())\r\n\r\nmat1= []\r\nfor i in range(r1):\r\n a= []\r\n for j in range(c1):\r\n a.append(int(input()))\r\n mat1.append(a)\r\n \r\nr2= int(input())\r\nc2= int(input())\r\n\r\nmat2= []\r\nfor i in range(r2):\r\n a= []\r\n for j in range(c2):\r\n a.append(int(input()))\r\n mat2.append(a)\r\n \r\nmatrixMult(mat1, mat2)"}},"points":10,"difficulty":"easy","sampleInput":"2\r\n3\r\n10\r\n0\r\n0\r\n0\r\n20\r\n0\r\n3\r\n4\r\n1\r\n0\r\n1\r\n0\r\n0\r\n1\r\n1\r\n2\r\n1\r\n1\r\n0\r\n0","sampleOutput":"10 0 10 0\r\n0 20 20 40","questionVideo":"https://www.youtube.com/embed/eLIT5fnKmMI","hints":[],"associated":[{"id":"24039ee4-5b57-4436-bfcd-bf5d4eee1e75","name":"(Matrix Multiplication) What is the time complexity for this question?","slug":"matrix-multiplication-what-is-the-time-complexity-for-this-question","type":4},{"id":"529be16d-9ca8-4314-8ae9-db31b388ad94","name":"(Matrix Multiplication) Matrix A is of order 3*4 and Matrix B is of order 4*5. How many elements will be there in a matrix A*B ?","slug":"matrix-multiplication-matrix-a-is-of-order-3-4-and-matrix-b-is-of-order-4-5-how-many-elements-will-be-there-in-a-matrix-a-b","type":4},{"id":"8830bd5c-1fbf-4ce0-a22c-a665dc397b44","name":"(Matrix Multiplication) For two matrices of size n1*m1 and n2*m2, what is the necessary condition for them to be able to be multiplied with each other?","slug":"matrix-multiplication-for-two-matrices-of-size-n1-m1-and-n2-m2-what-is-the-necessary-condition-for-them-to-be-able-to-be-multiplied-with-each-other","type":4},{"id":"a61e95dc-1ad9-46e5-a09e-f869ad8aa062","name":"(Matrix Multiplication) Is it possible to solve this question with lower time complexity?","slug":"matrix-multiplication-is-it-possible-to-solve-this-question-with-lower-time-complexity","type":4}],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"f185374c-5448-476e-93e8-c4a4e9d1d520","name":"2D Arrays","slug":"2d-arrays-9999","type":0},{"id":"d5c0c53a-ef6d-4619-81d7-94fd161edaa3","name":"Matrix Multiplication","slug":"matrix-multiplication","type":1}],"next":{"id":"beea55a6-71b3-4ace-a46e-32baca36383f","name":"Matrix Multiplication","type":3,"slug":"matrix-multiplication"},"prev":{"id":"bb2ced46-16da-4aac-bb90-06fe777ce474","name":"2D Arrays Demo","type":3,"slug":"2d-arrays-demo"}}}

Matrix Multiplication

<p>1. You are given a number n1, representing the number of rows of 1st matrix.</p><p>2. You are given a number m1, representing the number of columns of 1st matrix.</p><p>3. You are given n1*m1 numbers, representing elements of 2d array a1.</p><p>4. You are given a number n2, representing the number of rows of 2nd matrix.</p><p>5. You are given a number m2, representing the number of columns of 2nd matrix.</p><p>6. You are given n2*m2 numbers, representing elements of 2d array a2.</p><p>7. If the two arrays representing two matrices of dimensions n1 * m1 and n2 * m2 can be multiplied, display the contents of product array as specified in output format.</p><p>8. If the two arrays can't be multiplied, print "Invalid input".</p>

{"id":"5b809e65-ca24-4020-82da-3bce88476328","name":"Matrix Multiplication","description":"<p>1. You are given a number n1, representing the number of rows of 1st matrix.</p><p>2. You are given a number m1, representing the number of columns of 1st matrix.</p><p>3. You are given n1*m1 numbers, representing elements of 2d array a1.</p><p>4. You are given a number n2, representing the number of rows of 2nd matrix.</p><p>5. You are given a number m2, representing the number of columns of 2nd matrix.</p><p>6. You are given n2*m2 numbers, representing elements of 2d array a2.</p><p>7. If the two arrays representing two matrices of dimensions n1 * m1 and n2 * m2 can be multiplied, display the contents of product array as specified in output format.</p><p>8. If the two arrays can't be multiplied, print \"Invalid input\".</p>","inputFormat":"<p>A number n1</p><p>A number m1</p><p>e11 e12.. e21 e22.. .. n1 * m1 number of elements of array a1</p><p>A number n2</p><p>A number m2</p><p>e11' e12'.. e21' e22'.. .. n2 * m2 number of elements of array a2</p>","outputFormat":"<p>e11'' e12'' e13'' .. e21'' e22'' e23'' .. .. elements of prd array</p>","constraints":"<p>1 &lt;= n1 &lt;= 10^2</p><p>1 &lt;= m1 &lt;= 10^2</p><p>-10^9 &lt;= e11, e12, .. n1 * m1 elements &lt;= 10^9</p><p>1 &lt;= n2 &lt;= 10^2</p><p>1 &lt;= m2 &lt;= 10^2</p><p>-10^9 &lt;= e11', e12', .. n2 * m2 elements &lt;= 10^9</p>","sampleCode":{"cpp":{"code":"#include<iostream>\r\n#include<vector>\r\n\r\nusing namespace std;\r\n\r\nvoid matrixMult(vector<vector<int>> A, vector<vector<int>> B){\r\n// Write your code here.\r\n}\r\n\r\nint main(){\r\n int r1;\r\n int c1;\r\n cin>>r1;\r\n cin>>c1;\r\n\r\n vector<vector<int>> mat1;\r\n for(int i= 0; i< r1; i++){\r\n vector<int> arr;\r\n for(int j= 0; j< c1; j++){\r\n int ele;\r\n cin>> ele;\r\n arr.push_back(ele);\r\n }\r\n mat1.push_back(arr);\r\n }\r\n\r\n int r2;\r\n int c2;\r\n cin>>r2;\r\n cin>>c2;\r\n\r\n vector<vector<int>> mat2;\r\n for(int i= 0; i< r2; i++){\r\n vector<int> arr;\r\n for(int j= 0; j< c2; j++){\r\n int ele;\r\n cin>> ele;\r\n arr.push_back(ele);\r\n }\r\n mat2.push_back(arr);\r\n }\r\n\r\n matrixMult(mat1, mat2);\r\n}"},"java":{"code":"import java.io.*;\r\nimport java.util.*;\r\n\r\npublic class Main{\r\n\r\npublic static void main(String[] args) throws Exception {\r\n // write your code here\r\n }\r\n\r\n}"},"python":{"code":"def main():\r\n\r\nif __name__ == \"__main__\":\r\n main()def matrixMult(mat1, mat2):\r\n# Write your code here. \r\n \r\nr1= int(input())\r\nc1= int(input())\r\n\r\nmat1= []\r\nfor i in range(r1):\r\n a= []\r\n for j in range(c1):\r\n a.append(int(input()))\r\n mat1.append(a)\r\n \r\nr2= int(input())\r\nc2= int(input())\r\n\r\nmat2= []\r\nfor i in range(r2):\r\n a= []\r\n for j in range(c2):\r\n a.append(int(input()))\r\n mat2.append(a)\r\n \r\nmatrixMult(mat1, mat2)"}},"points":10,"difficulty":"easy","sampleInput":"2\r\n3\r\n10\r\n0\r\n0\r\n0\r\n20\r\n0\r\n3\r\n4\r\n1\r\n0\r\n1\r\n0\r\n0\r\n1\r\n1\r\n2\r\n1\r\n1\r\n0\r\n0","sampleOutput":"10 0 10 0\r\n0 20 20 40","questionVideo":"https://www.youtube.com/embed/eLIT5fnKmMI","hints":[],"associated":[{"id":"24039ee4-5b57-4436-bfcd-bf5d4eee1e75","name":"(Matrix Multiplication) What is the time complexity for this question?","slug":"matrix-multiplication-what-is-the-time-complexity-for-this-question","type":4},{"id":"529be16d-9ca8-4314-8ae9-db31b388ad94","name":"(Matrix Multiplication) Matrix A is of order 3*4 and Matrix B is of order 4*5. How many elements will be there in a matrix A*B ?","slug":"matrix-multiplication-matrix-a-is-of-order-3-4-and-matrix-b-is-of-order-4-5-how-many-elements-will-be-there-in-a-matrix-a-b","type":4},{"id":"8830bd5c-1fbf-4ce0-a22c-a665dc397b44","name":"(Matrix Multiplication) For two matrices of size n1*m1 and n2*m2, what is the necessary condition for them to be able to be multiplied with each other?","slug":"matrix-multiplication-for-two-matrices-of-size-n1-m1-and-n2-m2-what-is-the-necessary-condition-for-them-to-be-able-to-be-multiplied-with-each-other","type":4},{"id":"a61e95dc-1ad9-46e5-a09e-f869ad8aa062","name":"(Matrix Multiplication) Is it possible to solve this question with lower time complexity?","slug":"matrix-multiplication-is-it-possible-to-solve-this-question-with-lower-time-complexity","type":4}],"solutionSeen":false,"tags":[],"meta":{"path":[{"id":0,"name":"home"},{"id":"0c54b191-7b99-4f2c-acb3-e7f2ec748b2a","name":"Data Structures and Algorithms","slug":"data-structures-and-algorithms","type":0},{"id":"f185374c-5448-476e-93e8-c4a4e9d1d520","name":"2D Arrays","slug":"2d-arrays-9999","type":0},{"id":"d5c0c53a-ef6d-4619-81d7-94fd161edaa3","name":"Matrix Multiplication","slug":"matrix-multiplication","type":1}],"next":{"id":"beea55a6-71b3-4ace-a46e-32baca36383f","name":"Matrix Multiplication","type":3,"slug":"matrix-multiplication"},"prev":{"id":"bb2ced46-16da-4aac-bb90-06fe777ce474","name":"2D Arrays Demo","type":3,"slug":"2d-arrays-demo"}}}
plane

Editor


Loading...

Matrix Multiplication

easy

1. You are given a number n1, representing the number of rows of 1st matrix.

2. You are given a number m1, representing the number of columns of 1st matrix.

3. You are given n1*m1 numbers, representing elements of 2d array a1.

4. You are given a number n2, representing the number of rows of 2nd matrix.

5. You are given a number m2, representing the number of columns of 2nd matrix.

6. You are given n2*m2 numbers, representing elements of 2d array a2.

7. If the two arrays representing two matrices of dimensions n1 * m1 and n2 * m2 can be multiplied, display the contents of product array as specified in output format.

8. If the two arrays can't be multiplied, print "Invalid input".

Constraints

1 <= n1 <= 10^2

1 <= m1 <= 10^2

-10^9 <= e11, e12, .. n1 * m1 elements <= 10^9

1 <= n2 <= 10^2

1 <= m2 <= 10^2

-10^9 <= e11', e12', .. n2 * m2 elements <= 10^9

Format

Input

A number n1

A number m1

e11 e12.. e21 e22.. .. n1 * m1 number of elements of array a1

A number n2

A number m2

e11' e12'.. e21' e22'.. .. n2 * m2 number of elements of array a2

Output

e11'' e12'' e13'' .. e21'' e22'' e23'' .. .. elements of prd array

Example

Sample Input

2 3 10 0 0 0 20 0 3 4 1 0 1 0 0 1 1 2 1 1 0 0

Sample Output

10 0 10 0 0 20 20 40

Question Video

Discussions

Show Discussion

Related Resources

related resources

Turning Off Zen Mode