🧠
In-Place Transpose by Swapping Elements
💡 This approach is more efficient because it transposes the matrix without extra space by swapping elements across the diagonal.
Intuition
Only swap elements above the diagonal with their corresponding elements below the diagonal to avoid double swapping.
Algorithm
- Iterate over rows from 0 to n-1.
- For each row, iterate over columns from i+1 to n-1 to avoid redundant swaps.
- Swap element at (i, j) with element at (j, i).
- Continue until all elements above the diagonal are swapped.
💡 Skipping the diagonal and lower triangle prevents undoing swaps and reduces operations.
def transpose(matrix):
n = len(matrix)
for i in range(n):
for j in range(i + 1, n):
matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]
# Driver code
if __name__ == '__main__':
mat = [[1,2,3],[4,5,6],[7,8,9]]
transpose(mat)
print(mat)
Line Notes
n = len(matrix)Get matrix size for loop bounds.
for i in range(n):Outer loop iterates over rows.
for j in range(i + 1, n):Inner loop iterates only over columns above the diagonal to avoid double swaps.
matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j]Swap elements symmetrically across the diagonal.
import java.util.Arrays;
public class Transpose {
public static void transpose(int[][] matrix) {
int n = matrix.length;
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
int temp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = temp;
}
}
}
public static void main(String[] args) {
int[][] mat = {{1,2,3},{4,5,6},{7,8,9}};
transpose(mat);
System.out.println(Arrays.deepToString(mat));
}
}
Line Notes
int n = matrix.length;Determine matrix size for iteration.
for (int i = 0; i < n; i++) {Outer loop over rows.
for (int j = i + 1; j < n; j++) {Inner loop over columns above diagonal to avoid redundant swaps.
int temp = matrix[i][j];Temporarily store element for swapping.
#include <iostream>
#include <vector>
using namespace std;
void transpose(vector<vector<int>>& matrix) {
int n = matrix.size();
for (int i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
swap(matrix[i][j], matrix[j][i]);
}
}
}
int main() {
vector<vector<int>> mat = {{1,2,3},{4,5,6},{7,8,9}};
transpose(mat);
for (auto& row : mat) {
for (auto& val : row) cout << val << ' ';
cout << '\n';
}
return 0;
}
Line Notes
int n = matrix.size();Get matrix dimension for loops.
for (int i = 0; i < n; i++) {Outer loop iterates over rows.
for (int j = i + 1; j < n; j++) {Inner loop iterates over columns above diagonal.
swap(matrix[i][j], matrix[j][i]);Swap elements across the diagonal in-place.
function transpose(matrix) {
const n = matrix.length;
for (let i = 0; i < n; i++) {
for (let j = i + 1; j < n; j++) {
[matrix[i][j], matrix[j][i]] = [matrix[j][i], matrix[i][j]];
}
}
}
// Test
const mat = [[1,2,3],[4,5,6],[7,8,9]];
transpose(mat);
console.log(mat);
Line Notes
const n = matrix.length;Get matrix size for iteration.
for (let i = 0; i < n; i++) {Outer loop over rows.
for (let j = i + 1; j < n; j++) {Inner loop over columns above diagonal to avoid double swaps.
[matrix[i][j], matrix[j][i]] = [matrix[j][i], matrix[i][j]];Swap elements in-place using destructuring assignment.
We only visit each element above the diagonal once and swap it, no extra space is used.
💡 For n=3, this means about 3 swaps and no extra memory, making it efficient.
Interview Verdict: Accepted and optimal for in-place transpose
This is the preferred approach in interviews because it uses no extra space and is easy to implement.