easy Transpose Matrix
Problem Statement
Given a 2D array matrix, return the transpose of the matrix.
In the transpose matrix, the first row of the original matrix becomes the first column, the second row becomes the second column, and so on.
Examples
- Example 1:
- Input:
[[1,2],
[3,4]]
- Expected Output:
[[1,3],
[2,4]]
-
Justification: The first row
[1,2]becomes the first column[1,3], and the second row[3,4]becomes the second column[2,4]. -
Example 2:
- Input:
[[5,6,7],
[8,9,10],
[11,12,13]]
- Expected Output:
[[5,8,11],
[6,9,12],
[7,10,13]]
-
Justification: The matrix is rearranged such that each row becomes a column in the transposed matrix.
-
Example 3:
- Input:
[[14,15,16,17],
[18,19,20,21],
[22,23,24,25],
[26,27,28,29]]
- Expected Output:
[[14,18,22,26],
[15,19,23,27],
[16,20,24,28],
[17,21,25,29]]
- Justification: Similar to the previous examples, each row of the original matrix is transformed into a column in the transposed matrix.
Try it yourself
Try solving this question here:
✅ Solution Transpose Matrix
Problem Statement
Given a 2D array matrix, return the transpose of the matrix.
In the transpose matrix, the first row of the original matrix becomes the first column, the second row becomes the second column, and so on.
Examples
- Example 1:
- Input:
[[1,2],
[3,4]]
- Expected Output:
[[1,3],
[2,4]]
-
Justification: The first row
[1,2]becomes the first column[1,3], and the second row[3,4]becomes the second column[2,4]. -
Example 2:
- Input:
[[5,6,7],
[8,9,10],
[11,12,13]]
- Expected Output:
[[5,8,11],
[6,9,12],
[7,10,13]]
-
Justification: The matrix is rearranged such that each row becomes a column in the transposed matrix.
-
Example 3:
- Input:
[[14,15,16,17],
[18,19,20,21],
[22,23,24,25],
[26,27,28,29]]
- Expected Output:
[[14,18,22,26],
[15,19,23,27],
[16,20,24,28],
[17,21,25,29]]
- Justification: Similar to the previous examples, each row of the original matrix is transformed into a column in the transposed matrix.
Solution
To solve this problem, we'll create a new matrix where the number of rows equals the number of columns in the original matrix, and vice versa. The key idea is to iterate through each element of the original matrix and assign it to the appropriate position in the new matrix. This approach ensures that all elements are moved to their correct positions in the transposed matrix.
We believe this method is effective because it directly addresses the definition of matrix transposition by rearranging elements based on their indices. It's efficient as it requires only a single pass through the original matrix, making it both straightforward and suitable for large matrices.
Step-by-step Algorithm
-
Initialize a new matrix for the transposed result. The dimensions of this new matrix will be the transpose of the original matrix's dimensions (i.e., if the original matrix has dimensions
n x m, the transposed matrix will have dimensionsm x n). -
Iterate through the rows of the original matrix. Use a loop that iterates from
0ton - 1, wherenis the number of rows in the original matrix. -
Iterate through the columns of the original matrix within each row iteration. Use a nested loop that iterates from
0tom - 1, wheremis the number of columns in the original matrix. -
Assign each element to its new position in the transposed matrix. During the nested iteration, for each element at position
(i, j)in the original matrix, assign its value to position(j, i)in the new transposed matrix. -
Return the transposed matrix as the output of the function.
Algorithm Walkthrough
Let's consider the input: matrix = [[5,6,7],[8,9,10],[11,12,13]]
Let's walk through the algorithm step by step using the provided example matrix.
-
Initialize the transposed matrix: Since the input matrix has dimensions
3 x 3(3 rows and 3 columns), we initialize a new matrix with the same dimensions3 x 3, as the input matrix is square. This new matrix starts as[[0,0,0],[0,0,0],[0,0,0]]. -
Iterate through the rows of the original matrix:
-
First iteration (i = 0): The focus is on the first row
[5,6,7].- Nested iteration for columns:
- j = 0: The first element (5) is placed in the transposed matrix at
(0, 0). The transposed matrix now is[[5,0,0],[0,0,0],[0,0,0]]. - j = 1: The second element (6) is placed in the transposed matrix at
(1, 0). The transposed matrix now is[[5,0,0],[6,0,0],[0,0,0]]. - j = 2: The third element (7) is placed in the transposed matrix at
(2, 0). The transposed matrix now is[[5,0,0],[6,0,0],[7,0,0]].
- j = 0: The first element (5) is placed in the transposed matrix at
- Nested iteration for columns:
-
Second iteration (i = 1): The focus shifts to the second row
[8,9,10].- Nested iteration for columns:
- j = 0: The first element (8) is placed in the transposed matrix at
(0, 1). The transposed matrix now is[[5,8,0],[6,0,0],[7,0,0]]. - j = 1: The second element (9) is placed in the transposed matrix at
(1, 1). The transposed matrix now is[[5,8,0],[6,9,0],[7,0,0]]. - j = 2: The third element (10) is placed in the transposed matrix at
(2, 1). The transposed matrix now is[[5,8,0],[6,9,0],[7,10,0]].
- j = 0: The first element (8) is placed in the transposed matrix at
- Nested iteration for columns:
-
Third iteration (i = 2): Now looking at the third row
[11,12,13].- Nested iteration for columns:
- j = 0: The first element (11) is placed in the transposed matrix at
(0, 2). The transposed matrix now is[[5,8,11],[6,9,0],[7,10,0]]. - j = 1: The second element (12) is placed in the transposed matrix at
(1, 2). The transposed matrix now is[[5,8,11],[6,9,12],[7,10,0]]. - j = 2: The third element (13) is placed in the transposed matrix at
(2, 2). The transposed matrix is now complete as[[5,8,11],[6,9,12],[7,10,13]].
- j = 0: The first element (11) is placed in the transposed matrix at
- Nested iteration for columns:
-
-
Return the transposed matrix: The final step is to return the fully transposed matrix, which in this case is
[[5,8,11],[6,9,12],[7,10,13]].
Code
public class Solution {
public int[][] transpose(int[][] matrix) {
// Determine the number of rows and columns in the input matrix.
int rows = matrix.length;
int cols = matrix[0].length;
// Initialize the transposed matrix with switched dimensions.
int[][] transposedMatrix = new int[cols][rows];
// Iterate through each element of the matrix.
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
// Assign the element to its new position in the transposed matrix.
transposedMatrix[j][i] = matrix[i][j];
}
}
// Return the transposed matrix.
return transposedMatrix;
}
public static void main(String[] args) {
Solution solution = new Solution();
// Test the transpose method with three example matrices.
// Example 1: 2x2 matrix
int[][] example1 = { { 1, 2 }, { 3, 4 } };
// Example 2: 3x3 matrix
int[][] example2 = { { 5, 6, 7 }, { 8, 9, 10 }, { 11, 12, 13 } };
// Example 3: 4x4 matrix
int[][] example3 = {
{ 14, 15, 16, 17 },
{ 18, 19, 20, 21 },
{ 22, 23, 24, 25 },
{ 26, 27, 28, 29 },
};
printMatrix(solution.transpose(example1));
printMatrix(solution.transpose(example2));
printMatrix(solution.transpose(example3));
}
private static void printMatrix(int[][] matrix) {
// Helper method to print the matrices.
for (int[] row : matrix) {
for (int val : row) {
System.out.print(val + " ");
}
System.out.println();
}
}
}
Complexity Analysis
Time Complexity
The time complexity is n is the number of rows and m is the number of columns in the input matrix. This is because each element in the n x m matrix is visited exactly once to place it in its new position in the transposed matrix.
Space Complexity
The space complexity is n x m. No additional space besides the output matrix is used, so the space complexity directly corresponds to the size of the output.
🤖 Don't fully get this? Learn it with Claude
Stuck on Transpose Matrix? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.
Progressively stronger hints — you still solve it.
I'm working on the problem **Transpose Matrix** (DSA). Give me a HINT LADDER: start with the tiniest nudge, then wait. Only reveal the next, stronger hint when I ask. Do NOT show the full solution unless I type 'show solution'. Keep me doing the thinking. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
See the technique, not just code.
Explain the optimal approach to **Transpose Matrix** with a VISUAL walkthrough: trace it on a small concrete example using ASCII art / a step-by-step diagram, narrate what changes each step, then give time & space complexity with a one-line derivation. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
Catch bugs, edge cases, sub-optimality.
I'll paste my solution to **Transpose Matrix**. Review it for correctness, missed edge cases, and time/space complexity, then coach me toward the optimal — don't just rewrite it. Ask me to paste my code now. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
Lock in recognition with look-alikes.
Give me 2 problems that use the SAME underlying pattern as **Transpose Matrix**. For each, let me attempt first, then review my answer and name the trigger signal that reveals the pattern. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.