#K59482. Matrix Rotation

    ID: 30874 Type: Default 1000ms 256MiB

Matrix Rotation

Matrix Rotation

You are given an n x n square matrix. Your task is to rotate the matrix by 90° clockwise in-place. This means you cannot use an additional matrix for the rotation.

The rotation process can be described in two steps:

  1. First, transpose the matrix. The transpose of a matrix is obtained by swapping matrix[i][j] with matrix[j][i] for all valid indices.
  2. Then, reverse each row of the transposed matrix.

Note: The indices in the matrix follow a 0-indexed convention. The rotation should be performed in place.

The mathematical explanation in \( \LaTeX \) is as follows: Given a matrix \( A \), its transpose \( A^T \) is defined by \( A^T_{ij} = A_{ji} \). After transposition, reversing each row results in the rotated matrix \( R \), where \( R_{ij} = A_{n-1-j,i} \) for \( 0 \leq i,j < n \).

inputFormat

The input is read from stdin and has the following format:

n
row_1_element_1 row_1_element_2 ... row_1_element_n
row_2_element_1 row_2_element_2 ... row_2_element_n
...
row_n_element_1 row_n_element_2 ... row_n_element_n

Where n is the size of the matrix.

outputFormat

Output the rotated matrix to stdout. Each row should be printed on a separate line with elements separated by a single space.

## sample
3
1 2 3
4 5 6
7 8 9
7 4 1

8 5 2 9 6 3

</p>