Loading...
Loading...
A shop has a matrix A of how many items of each kind each customer bought (customers × items) and a matrix B of the price of each item in each currency (items × currencies). The product A × B gives each customer's bill in each currency.
Multiply two matrices A (r1 × c1) and B (r2 × c2). This is possible only when c1 equals r2. Each answer cell [i][j] is the sum of A[i][k] × B[k][j] over all k.
NOT POSSIBLE.Example 1
Input
2 3 1 2 3 4 5 6 3 2 7 8 9 10 11 12
Output
58 64 139 154
Top-left cell: 1×7 + 2×9 + 3×11 = 58.
Example 2
Input
2 2 1 2 3 4 3 1 1 2 3
Output
NOT POSSIBLE
A has 2 columns but B has 3 rows.
Try the problem first. Open one hint at a time.
Read both matrices first, then check c1 == r2.
The answer has r1 rows and c2 columns. Use three nested loops: row i, column j, and k to add up A[i][k] × B[k][j].
One product can reach 10^12, so keep the sums in long and cast before multiplying.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor