You are given a rectangular grid of integers with r rows and c columns. Produce its transpose: a grid with c rows and r columns where the value in row i, column j of the output equals the value in row j, column i of the input. In other words, the first output row is the first input column (top to bottom), the second output row is the second input column, and so on.
Line 1: two integers r and c separated by a space.
Next r lines: each contains c space-separated integers, giving the grid row by row.
Print c lines. Line i (1-indexed) contains r space-separated integers: the i-th column of the input grid, from top to bottom.
Wide grid
Input
2 3 1 2 3 4 5 6
Expected
1 4 2 5 3 6
Explanation
The input has 2 rows and 3 columns. Its columns are (1,4), (2,5), (3,6), which become the three rows of the 3×2 transpose.
Tall grid
Input
3 1 7 8 9
Expected
7 8 9
Explanation
A single column (7,8,9) becomes a single output row `7 8 9`.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →