#C6148. Latin Square Transformation
Latin Square Transformation
Latin Square Transformation
You are given an integer n and an n x n grid of integers. Your task is to transform the given grid into a Latin square of size n x n using the following procedure:
- Sort the first row of the grid in non-decreasing order.
- For i from
0
ton-1
, produce the i-th row of the output by rotating the sorted first row to the left by i positions.
For example, if the sorted first row is \( [a_1,a_2,\dots,a_n] \), then the i-th row is:
[ [a_{i+1}, a_{i+2}, \dots, a_n, a_1, \dots, a_i] ]
This process guarantees that every row is a cyclic permutation of the sorted first row, forming a Latin square.
Note: The input grid is provided for conformity, but only the first row is used to generate the output Latin square.
inputFormat
The first line contains a single integer n (\(1 \leq n \leq 100\)). Each of the next n lines contains n space separated integers, representing the grid.
The grid is guaranteed to have its first row containing n distinct integers.
outputFormat
Output the resultant Latin square. Print n lines with n space separated integers each. Each line represents a row of the Latin square.
## sample3
1 2 3
3 1 2
2 3 1
1 2 3
2 3 1
3 1 2
</p>