#K91522. Sorted Matrix Rearrangement
Sorted Matrix Rearrangement
Sorted Matrix Rearrangement
Given an \(n \times m\) matrix, rearrange its elements so that the resulting matrix is sorted in non-decreasing order along each row and column. You can achieve this by flattening the matrix into a single list, sorting it, and then reassigning the sorted values row by row.
Example:
Input: 3 3 1 3 5 2 4 6 7 8 9</p>Output: 1 2 3 4 5 6 7 8 9
In the above example, all elements are sorted in ascending order when the matrix is reformed.
inputFormat
The first line of input contains two integers (n) and (m), representing the number of rows and columns respectively. Each of the following (n) lines contains (m) space-separated integers representing the matrix elements.
outputFormat
Output the rearranged matrix. Each of the (n) rows should be printed on a new line, and the elements in a row must be separated by a space.## sample
3 3
1 2 3
4 5 6
7 8 9
1 2 3
4 5 6
7 8 9
</p>