## Problem Definition – Matrix Rotation (by 90, 180, 270 degrees)

This is a very famous interview question and has been asked numerous times. We are trying to solve the problem of matrix rotations where the inputs are as follows:

- A matrix of dimension M * N
- A number from the set (90, 180, ,270) by which we need to rotate the matrix.

For simplicity we will consider a matrix to be a 2 dimensional array of integers.

##### What does it mean to rotate a matrix?

This can only be explained by nice diagrams. Just for a formal definition, I would say that matrix rotation is a structural re-arrangement of the rows and columns of the matrix and it is achieved by a sequence of operations on the matrix as a whole. Depending on the degrees by which we want to rotate, the permutations of the operations may vary.

## Prerequisites – Matrix Rotations

A necessary prerequisite for the matrix rotations is to have knowledge about matrix transpose. So, let us first talk about transpose.

A transpose of a matrix A(M * N) is represented by A^{T} and the dimensions of A^{T} is N * M.

Here is an image to demonstrate the transpose of a given matrix.

The red boundaries (rows) in the matrix A becomes the column in A^{T} and the blue boundaries (columns) in matrix B becomes the row in B^{T}.

In simple words, if we can exchange each column with each of the rows in order (1st column with first row, second column with second row and so on..) then at the end of the iteration we will get the transpose of the matrix.

##### Horizontal Reflection

This precisely means exchanging one row with the other such that the first row becomes the last, the second row becomes the second last and so on..

For a matrix of dimension M * N there are M rows and N columns and horizontal reflection means Row M is exchanged with Row 1, Row M-1 is exchange with Row 2 so on. If the value of M is odd then the position of row 1 + M/2 remains intact and it is not changed, that is why it is also called horizontal reflection through middle row.

Here is an example of horizontal reflection.

##### Vertical Reflection

This precisely means exchanging one column with the other such that the first column becomes the last, the second column becomes the second last and so on..

For a matrix of dimension M * N there are M rows and N columns and vertical reflection means Column N is exchanged with Column 1, Column N-1 is exchange with Column 2 so on. If the value of N is odd then the position of Column 1 + N/2 remains intact and it is not changed, that is why it is also called vertical reflection through the middle column.

Here is an example of vertical reflection.

##### Steps for Matrix Rotation – 90 degrees

- Take transpose of the matrix.
- Take reflection of the transpose against the horizontal axis.

##### Steps for Matrix Rotation – 180 degrees

- Take reflection of the matrix against the horizontal axis.
- Take reflection of the resultant matrix against the vertical axis.

##### Steps for Matrix Rotation – 270 degrees

- Take transpose of the matrix.
- Take reflection of the transpose against the vertical axis.

## Source Code – Matrix Rotation

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
public class Matrix { public static void main(String[] args) { Matrix m = new Matrix(); int[][] matrix = new int[][] { { 1, 6, 11, 16, 21 }, { 2, 7, 12, 17, 22 }, { 3, 8, 13, 18, 23 }, { 4, 9, 14, 19, 24 }, { 5, 10, 15, 20, 25 } }; int[][] transpose = m.transpose(matrix); System.out.println("Transpose of Matrix"); m.printMatrix(transpose); matrix = new int[][] { { 1, 6, 11, 16, 21 }, { 2, 7, 12, 17, 22 }, { 3, 8, 13, 18, 23 }, { 4, 9, 14, 19, 24 }, { 5, 10, 15, 20, 25 } }; m.horizontalReflection(matrix); System.out.println("Horizontal Reflection of Matrix"); m.printMatrix(matrix); matrix = new int[][] { { 1, 6, 11, 16, 21 }, { 2, 7, 12, 17, 22 }, { 3, 8, 13, 18, 23 }, { 4, 9, 14, 19, 24 }, { 5, 10, 15, 20, 25 } }; m.verticalReflection(matrix); System.out.println("Vertical Reflection of Matrix"); m.printMatrix(matrix); matrix = new int[][] { { 1, 6, 11, 16, 21 }, { 2, 7, 12, 17, 22 }, { 3, 8, 13, 18, 23 }, { 4, 9, 14, 19, 24 }, { 5, 10, 15, 20, 25 } }; transpose = m.transpose(matrix); m.horizontalReflection(transpose); System.out.println("90 degree rotation of Matrix"); m.printMatrix(transpose); m.horizontalReflection(matrix); m.verticalReflection(matrix); System.out.println("180 degree rotation of Matrix"); m.printMatrix(matrix); transpose = m.transpose(matrix); m.verticalReflection(transpose); System.out.println("270 degree rotation of Matrix"); m.printMatrix(transpose); } /* * Method to get transpose of a matrix. It creates a new matrix to copy the * elements because the physical structure of the matrix may change as a * part of the transpose operation in case its not a square matrix. */ public int[][] transpose(int[][] mat) { if (mat == null) return null; int m = mat.length; int n = mat[0].length; int[][] matResult = new int[n][m]; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { matResult[j][i] = mat[i][j]; } } return matResult; } /* * Method to get the horizontal reflection of the matrix. */ public void horizontalReflection(int[][] mat) { int m = mat.length; int n = mat[0].length; int temp = 0; for (int i = 0; i < m / 2; i++) { for (int j = 0; j < n; j++) { temp = mat[i][j]; mat[i][j] = mat[m - (i + 1)][j]; mat[m - (i + 1)][j] = temp; } } } /* * Method to get the vertical reflection of the given matrix */ public void verticalReflection(int[][] mat) { int m = mat.length; int n = mat[0].length; int temp = 0; for (int i = 0; i < n / 2; i++) { for (int j = 0; j < m; j++) { temp = mat[j][i]; mat[j][i] = mat[j][n - (i + 1)]; mat[j][n - (i + 1)] = temp; } } } /* * Utility method to print the matrix. */ public void printMatrix(int[][] mat) { int m = mat.length; int n = mat[0].length; for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { System.out.print(mat[i][j] + " "); } System.out.println(); } } } |

Don’t forget to subscribe to TechieMe to get updates on latest posts.

Here is a Matrix Rotation WIKI for the topic