PIXELBANKv8.2.1
Menu

Reshape the Matrix

Given an m x n matrix, reshape it to r x c. If not possible, return the original matrix.

Output each row on a line, space-separated.

Example:

Input:
1,2
3,4
1 4
Output:
1 2 3 4
Reasoning:
  • The input matrix is 1×21 \times 2, with values 1,2 and 3,4, but the actual matrix is defined by the first line as a 1×21 \times 2 matrix with values 1 and 2, and the second line as a 1×21 \times 2 matrix with values 3 and 4.
  • The target reshape dimensions are given as 1 4, meaning 11 row and 44 columns, but since the total number of elements in the original matrix is 44 (22 elements per row ×\times 22 rows), we can reshape it to 1×41 \times 4.
  • We then flatten the original matrix into a single array: [1, 2, 3, 4].
  • The final output is this flattened array reshaped into 1×41 \times 4 dimensions: 1 2 3 4

Constraints:

  • 1 <= m, n <= 100
Editor

Test Results

0/0
Run code to see test results.