Skills101
← All coding problems

Transpose a Matrix

easyMatrix+10 XPTime limit 2s

The transpose of a matrix turns its rows into columns: row i of the original becomes column i of the result. An r × c matrix becomes a c × r matrix.

Input:

  • line 1: r c
  • next r lines: c integers each

Output: the transposed matrix — c lines with r integers each.

Constraints: 1 ≤ r, c ≤ 500, −10⁹ ≤ value ≤ 10⁹.

Think about it: the value at row i, column j of the result is a[j][i]. For a non-square matrix you need a new c × r array — swapping in place only works for squares.

Sample input 1

2 3
1 2 3
4 5 6

Sample output 1

1 4
2 5
3 6

Sample input 2

1 1
7

Sample output 2

7

Read input from standard input and print the answer to standard output. Your code runs in a secure sandbox against the samples (Run) and hidden tests (Submit).

Loading editor…

Log in to run and submit your code — it's free.