Mirror Matrix
Given a square matrix (2D array), return a new matrix which is the mirror image of the original matrix across its vertical center line. This means elements in the first column of the input matrix are swapped with the corresponding elements in the last column, the second column swapped with the second-to-last column, etc.
[ [ 1, 2 ], [ 3, 4 ] ]
Explanation. The matrix is 2x2. We swap the first column with the second column to get the mirrored matrix.
[ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ] ]
Explanation. The matrix is 3x3. Each row is reversed to get the mirror image.
[ [ 1 ] ]
Explanation. The matrix is 1x1. Mirroring does not change the matrix.
[ [ 10, 20, 30, 40 ], [ 50, 60, 70, 80 ], [ 90, 100, 110, 120 ], [ 130, 140, 150, 160 ] ]
Explanation. The matrix is 4x4. Columns are swapped with their corresponding counterparts from the end towards the center.
Follow-up: Can this transformation be done in place, without using extra space for another matrix?
The matrix will always be square (i.e., same number of rows and columns) and will contain only integers. The size of the matrix will be at least 1x1 and at most 10x10.
- Views
- 2