Array ( [0] => [1] => questions [2] => Popular-Algorithms [3] => Transpose-of-given-Matrix ) Popular Algorithms | Transpose of given Matrix | THE INQUISITIVE





Transpose of given Matrix

LEVEL:Beginner

Description

Given a 2D array of elements. You have to transpose of given Matrix

Input Format

First line contains two integers M and N. Next M lines contains N integers separated by spaces.

Output Format

Print the resultant Matrix.


Example 1:

Input
3 3
1 2 3
4 5 6
7 8 9
Output
1 4 7
2 5 8
3 6 9
Example 2:

Input
2 2
1 2 
3 4
Output
1 3
2 4
Example 3:

Input
3 3
3 2 1
6 5 4
9 8 7
Output
3 6 9
2 5 8
1 4 7

oops

Login to see Discussion




Approach

Step-i) create a new array of same size ( here string has been used for the simplicity of printing in main method without
using any loops over there)
Step-ii)run loop from i = 0 to iStep-iii) run other loop from j=0 to jStep-iv) append data of given array[j][i] to our created array[i][j] (here it is string)
Step-v) return the array ( string has been returned here)

Time Complexity: O(n^2)
Space Complexity: O(n^2)
Note : these are for a matrix of n*n if it is a rectangle matrix then the it can be modified as O(m*n)


Note :

Let us know if you can come up with a better approach, mail us at support@theinquisitive.in Your approach will be reviewed and posted with credits to you.

oops

Login to see Solution