The Matrix Reshaper

EasyAcc. 95.4%
+20 XP 5

Dimension Morphing

If the total number of elements matches, you can "reflow" any matrix into another shape.

The Assignment

Your function receives mat, r, and c.

  1. Check if rows * cols === r * c. If not, print the original rows.
  2. Flatten the matrix and refill it into r rows and c columns.
  3. Print the final matrix row by row, with elements separated by spaces. If the reshape is invalid, print the original matrix.

01EXAMPLE 1

Inputmat=[[1,2],[3,4]], r=1, c=4
Output1 2 3 4

Explanation: Flattened to one row.

Constraints

  • Validation check required.
Matrix
JavaScript
Loading...
1 Hidden

Input Arguments

mat[[1,2],[3,4]]
r1
c4

Expected Output

1 2 3 4

Click RUN to test your solution