Easy · Flat Index Mapping
Understand → Visualise → Practice.
Reshape while preserving row-major order; if dimensions are incompatible, return the original matrix.
Reading the matrix row by row gives a flat sequence; the k-th value goes to row k // cols and column k % cols of the new shape.
If rows × cols differs from the number of cells, return the original matrix.
List all values row by row.
Take consecutive slices of length cols.
O(rows × cols) time and O(rows × cols) space.
Reshaping column by column changes the order of values.
Read the full guide first: Matrices and 2-D Arrays.
Guest mode lets you understand, visualise and run the sample tests. Create a free account to submit and save your solutions.