Transpose of Matrix | gfg | solution|

Transpose Matrix

Transpose matrix


Problem Statement 

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.

Example 1:

Input:
N = 4
mat[][] = {{1, 1, 1, 1},
           {2, 2, 2, 2}
           {3, 3, 3, 3}
           {4, 4, 4, 4}}
Output: 
{{1, 2, 3, 4},  
 {1, 2, 3, 4}  
 {1, 2, 3, 4}
 {1, 2, 3, 4}} 

Example 2:

Input:
N = 2
mat[][] = {{1, 2},
           {-9, -2}}
Output:
{{1, -9}, 
 {2, -2}}

Your Task:

You don't need to read input or print anything. Complete the function transpose() which takes matrix[][] and N as input parameters 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. 

Expected Time Complexity: O(N * N)
Expected Auxiliary Space: O(1)

Constraints:
1 <= N <= 100
-103 <= mat[i][j] <= 103


Solution :


l In this problem statement a square matrix is given of size N so the size of the matrix is greater than 1 and less than equal to 100  and we have to change the rows to columns and columns to rows. In place of rows, we have to print the values of columns.

Approach:

 we have to run the loop and again run another loop inside the loop  so we are here using the nested for 

for loop to solve this problem and then we have to swap the mat[i][j] to mat[j][i] and return the value 

and hence we are now able to print the transpose of the matrix.


Here my i is the index of the first loop and j is the index starting from zero is the index of the second loop which is inside the first loop 


Given  matrix=[ [1, 1, 1, 1,]

                         [2, 2, 2, 2,]

                         [3, 3, 3, 3,]

                          [4, 4, 4, 4,] ]

                  

Now indexes of the matrix

      [00, 01, 02, 03]

      [10, 11, 12, 13]

      [20, 21, 22, 23]

      [30, 31, 32, 33]


 [  [ij, ij, ij, ij]

   [ij, ij, ij, ij]

   [ij, ij, ij, ij]

   [ij, ij, ij, ij] ]

  

if we swap the value of mat[ij[j to matrix[j][i]

and here the value of j is swapped to the i and the value we got [j][i]

will transpose to the column


  class Solution


{

    //Function to find the transpose of a matrix.

    transpose(matrix, n)

    {

        // code here

     let k=0;

     for(let i=0; i<=matrix.length-1; i++){

         

         for(let j=k; j<=matrix.length-1; j++){

             

             let swap=matrix[i][j];

              matrix[i][j]=matrix[j][i];

              matrix[j][i]=swap;

                //Here my element will swapped 

         }

          k++;

     }

    }

}


Time Complexity

The time complexity of this solution is O(N^2), where N is the size of the input square matrix. This is because we traverse the matrix once, performing constant-time operations at each element.


Space Complexity

The space complexity of this solution is O(1), as we perform all operations in place without using any additional data structures that grow with the size of the input matrix.


Conclusion

By efficiently traversing the matrix and swapping elements, we can find the transpose of the given square matrix in place. This approach ensures simplicity and optimal performance, meeting the problem's constraints effectively.



Post a Comment

Previous Post Next Post