Array ( [0] => [1] => questions [2] => HackwithInfy-Previous-Questions [3] => Transpose-of-given-Matrix )
Given a 2D array of elements. You have to transpose of given Matrix
First line contains two integers M and N. Next M lines contains N integers separated by spaces.
Print the resultant Matrix.
3 3 1 2 3 4 5 6 7 8 9
1 4 7 2 5 8 3 6 9
2 2 1 2 3 4
1 3 2 4
3 3 3 2 1 6 5 4 9 8 7
3 6 9 2 5 8 1 4 7
Login to see Discussion
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 i
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.
Login to see Solution