Reshape the Matrix

Updated on 04 July, 2025
Reshape the Matrix header image

Problem Statement

In many scientific computing environments, such as MATLAB, there is a functionality to reshape matrices, which is useful in reorganizing data without changing the actual data. This concept is what we will explore in this problem. Specifically, you are provided with a matrix 'mat' defined by dimensions m x n. Using this matrix, you need to generate a new matrix of size r x c using the elements from 'mat'. The elements should be filled into the reshaped matrix in the same order they appear by rows in the original matrix.

The challenge is that the reshaping operation can only be performed if the number of elements in the original matrix (m * n) equals the number of elements in the desired reshaped matrix (r * c). If this condition is not met, the function should return the original matrix instead of a reshaped version. This problem tests your ability to understand matrix traversal and transformation based on conditional operations.

Examples

Example 1

Input:

mat = [[1,2],[3,4]], r = 1, c = 4

Output:

[[1,2,3,4]]

Example 2

Input:

mat = [[1,2],[3,4]], r = 2, c = 4

Output:

[[1,2],[3,4]]

Constraints

  • m == mat.length
  • n == mat[i].length
  • 1 <= m, n <= 100
  • -1000 <= mat[i][j] <= 1000
  • 1 <= r, c <= 300

Approach and Intuition

The core of this problem involves a conditional check and then either a transformation operation or a direct return of the input. The main steps to solve this problem can be outlined as follows:

  1. First, calculate the total number of elements in the original matrix mat by multiplying its dimensions (i.e., m * n).
  2. Calculate the total number of elements that would be in the reshaped matrix by multiplying r * c.
  3. If these two numbers are not the same, return the original matrix. This step ensures that each element of the original matrix can be mapped to a new position in the reshaped matrix without any leftovers or shortages.
  4. If the numbers do match, proceed to fill the reshaped matrix. Do this by traversing the elements of the original matrix in row-wise order (left to right, top to bottom) and insert these elements into the new matrix in the same order.
  5. Output the reshaped matrix.

From the example given:

  • In the first example, mat = [[1,2],[3,4]], r = 1, c = 4, reshaping is possible as m * n = 4 which is equal to r * c = 4. The elements are reorganized in a single row.
  • In the second example, mat = [[1,2],[3,4]], r = 2, c = 4, the reshaping is not possible because m * n = 4 is not equal to r * c = 8. Here, we return the original matrix.

This approach ensures that our reshaping operation adheres to the constraints imposed by the dimensions of the original and desired matrices.

Solutions

  • Java
java
public class Solution {
    public int[][] reshapeMatrix(int[][] matrix, int newRows, int newCols) {
        int[][] reshaped = new int[newRows][newCols];
        if (matrix.length == 0 || newRows * newCols != matrix.length * matrix[0].length)
            return matrix;
        int elements = 0;
        for (int row = 0; row < matrix.length; row++) {
            for (int col = 0; col < matrix[0].length; col++) {
                reshaped[elements / newCols][elements % newCols] = matrix[row][col];
                elements++;
            }
        }
        return reshaped;
    }
}

The provided Java code defines a method to reshape a given matrix into a new matrix with specified dimensions. Ensure the new dimensions maintain the same total number of elements as the original matrix, or the method returns the original matrix unchanged.

  • The code starts by verifying if the matrix is empty or the product of the new dimensions doesn't match the product of the original dimensions.
  • It initializes a counter elements to track the number of elements processed.
  • It iterates over each element in the original matrix, fills the reshaped matrix in row-major order using the division and modulus operations to find the correct indices in the reshaped matrix.
  • Finally, the reshaped matrix is returned.

Comments

No comments yet.