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.
- Check if
rows * cols === r * c. If not, print the original rows. - Flatten the matrix and refill it into
rrows andccolumns. - Print the final matrix row by row, with elements separated by spaces. If the reshape is invalid, print the original matrix.
01EXAMPLE 1
Input
mat=[[1,2],[3,4]], r=1, c=4Output
1 2 3 4Explanation: Flattened to one row.
Constraints
- Validation check required.
Matrix
JavaScriptSystem handles I/O — write your function only
Loading...
1 Hidden
Input Arguments
mat[[1,2],[3,4]]
r1
c4
Expected Output
1 2 3 4
Click RUN to test your solution