|
| 1 | +--- |
| 2 | +id: transpose-of-matrix |
| 3 | +title: Transpose Of Matrix |
| 4 | +sidebar_label: Transpose-Of-Matrix |
| 5 | +tags: |
| 6 | + - Matrix |
| 7 | + - Data Structure |
| 8 | +description: "This tutorial covers the solution to the Transpose of Matrix problem from the GeeksforGeeks." |
| 9 | +--- |
| 10 | +## Problem Description |
| 11 | + |
| 12 | +Write a program to find the transpose of a square matrix of size `N*N`. Transpose of a matrix is obtained by changing rows to columns and columns to rows. |
| 13 | + |
| 14 | +## Examples |
| 15 | + |
| 16 | +**Example 1:** |
| 17 | + |
| 18 | +``` |
| 19 | +Input: |
| 20 | +N = 4 |
| 21 | +mat[][] = {{1, 1, 1, 1}, |
| 22 | + {2, 2, 2, 2} |
| 23 | + {3, 3, 3, 3} |
| 24 | + {4, 4, 4, 4}} |
| 25 | +Output: |
| 26 | +{{1, 2, 3, 4}, |
| 27 | + {1, 2, 3, 4} |
| 28 | + {1, 2, 3, 4} |
| 29 | + {1, 2, 3, 4}} |
| 30 | +``` |
| 31 | + |
| 32 | +**Example 2:** |
| 33 | + |
| 34 | +``` |
| 35 | +Input: |
| 36 | +N = 2 |
| 37 | +mat[][] = {{1, 2}, |
| 38 | + {-9, -2}} |
| 39 | +Output: |
| 40 | +{{1, -9}, |
| 41 | + {2, -2}} |
| 42 | +``` |
| 43 | + |
| 44 | +## Your Task |
| 45 | +You dont need to read input or print anything. Complete the function transpose() which takes matrix[][] and N as input parameter and finds the transpose of the input matrix. You need to do this in-place. That is you need to update the original matrix with the transpose. |
| 46 | + |
| 47 | + |
| 48 | +## Constraints |
| 49 | + |
| 50 | +* `-10^9 <= mat[i][j] <= 10^9` |
| 51 | + |
| 52 | +## Problem Explanation |
| 53 | + |
| 54 | +The task is to traverse the matrix and transpose it. |
| 55 | + |
| 56 | +## Code Implementation |
| 57 | + |
| 58 | +### C++ Solution |
| 59 | + |
| 60 | +```cpp |
| 61 | + |
| 62 | +#include <vector> |
| 63 | +#include <algorithm> |
| 64 | + |
| 65 | +std::vector<std::vector<int>> transposeMatrix(const std::vector<std::vector<int>>& matrix) { |
| 66 | + int rows = matrix.size(); |
| 67 | + int cols = matrix[0].size(); |
| 68 | + std::vector<std::vector<int>> transpose(cols, std::vector<int>(rows)); |
| 69 | + for (int i = 0; i < rows; ++i) { |
| 70 | + for (int j = 0; j < cols; ++j) { |
| 71 | + transpose[j][i] = matrix[i][j]; |
| 72 | + } |
| 73 | + } |
| 74 | + return transpose; |
| 75 | +} |
| 76 | +``` |
| 77 | +
|
| 78 | +```java |
| 79 | +public static int[][] transposeMatrix(int[][] matrix) { |
| 80 | + int rows = matrix.length; |
| 81 | + int cols = matrix[0].length; |
| 82 | + int[][] transpose = new int[cols][rows]; |
| 83 | + for (int i = 0; i < rows; ++i) { |
| 84 | + for (int j = 0; j < cols; ++j) { |
| 85 | + transpose[j][i] = matrix[i][j]; |
| 86 | + } |
| 87 | + } |
| 88 | + return transpose; |
| 89 | +} |
| 90 | +
|
| 91 | +
|
| 92 | +``` |
| 93 | + |
| 94 | +```python |
| 95 | + |
| 96 | +def transpose_matrix(matrix): |
| 97 | + return list(zip(*matrix)) |
| 98 | + |
| 99 | +``` |
| 100 | + |
| 101 | +```javascript |
| 102 | +function transposeMatrix(matrix) { |
| 103 | + return matrix[0].map((_, colIndex) => matrix.map(row => row[colIndex])); |
| 104 | +} |
| 105 | + |
| 106 | +``` |
| 107 | + |
| 108 | +## Solution Logic: |
| 109 | + |
| 110 | +1. Create an empty matrix (2D array) to store the transposed matrix. |
| 111 | +2. Iterate through the original matrix: |
| 112 | + - For each element in the original matrix, swap its row and column indices to get the corresponding element in the transposed matrix. |
| 113 | + - Assign the value of the original element to the transposed element. |
| 114 | +3. Return the transposed matrix. |
| 115 | + |
| 116 | + |
| 117 | +## Time Complexity |
| 118 | + |
| 119 | +* The time complexity is $O(n*m)$ as where n is the number of rows in the matrix and m is the number of columns. This is because the solution iterates through each element in the matrix once. |
| 120 | + |
| 121 | + |
| 122 | +## Space Complexity |
| 123 | + |
| 124 | +* The space complexity of the solution is O(n*m), where n is the number of rows in the matrix and m is the number of columns. This is because the solution creates a new matrix to store the transposed matrix, which has the same size as the original matrix. |
0 commit comments