Problem Statement
Pattern:
Solution
public int[][] matrixReshape(int[][] mat, int r, int c) {
int n = mat.length, m = mat[0].length;
int[][] result = new int[r][c];
// if invalid array return original
if (r * c != n * m) return mat;
for (int i = 0; i < r*c; i++)
result[i/c][i%c] = mat[i/m][i%m];
return result;
}
Notes
- total no. of elements is
r*c
- row and column are given by dividing and mod by no. of columns