All problems

Problem 38

Transpose Matrix

Easy · 10 pts · Array, Matrix

Problem

You are given a matrix with R rows and C columns. Print its transpose: the matrix with C rows and R columns obtained by turning every row into a column. The element at row i, column j of the original moves to row j, column i of the transpose. The matrix does not have to be square, so the output has a different shape from the input whenever R and C differ.

Input

First line: integers R and C. Next R lines: C space-separated integers each.

Output

Print C lines with R space-separated integers each.

Constraints

1 <= R, C <= 100; -1000 <= value <= 1000

Example input

2 3
1 2 3
4 5 6

Example output

1 4
2 5
3 6

Explanation

The input 1 2 3 / 4 5 6 has 2 rows and 3 columns. Its columns are 1 4, 2 5 and 3 6, and these become the 3 rows of the transpose.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Rotate Matrix Next: Leaders in an Array