63. Multiply Two Matrices
Challenge1000 ms256 MBSolved by 0%
Read two matrices and print their product.
Input ``` r1 c1 (r1 rows of c1 values) r2 c2 (r2 rows of c2 values) ```
If c1 does not equal r2 the matrices cannot be multiplied — print Invalid.
Otherwise the result has r1 rows and c2 columns, and the value at row i, column
j is the sum of A[i][k] × B[k][j] over every k.
Constraints - `1 ≤ dimensions ≤ 100` - `-1000 ≤ each value ≤ 1000`
Input
The first line contains r1 and c1.
The next r1 lines hold the first matrix.
The following line contains r2 and c2.
The next r2 lines hold the second matrix.
Output
Print r1 lines of c2 integers each, separated by single spaces, or Invalid if the shapes cannot be multiplied.
Input2 3
1 2 3
4 5 6
3 2
7 8
9 10
11 12
Output58 64
139 154
Noteis a standard multiplication
Input2 2
1 2
3 4
3 2
1 2
3 4
5 6
OutputInvalid
Notehas mismatched inner dimensions, so no product exists
Hint 1Approach
Hint 2Approach
Hint 3Pseudocode
Hint 4Full solution