The Wayback Machine - https://web.archive.org/web/20240907054640/https://www.geeksforgeeks.org/program-to-find-transpose-of-a-matrix/
Open In App

Program to find transpose of a matrix

Last Updated : 12 Oct, 2023
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow
Companies:
Show Topics
Solve Problem
Easy
66.5%
1L

Given a matrix of size N X M, find the transpose of the matrix
Transpose of a matrix is obtained by changing rows to columns and columns to rows. In other words, transpose of A[N][M] is obtained by changing A[i][j] to A[j][i].

Example:

matrix-transpose

Approach: Follow the given steps to solve the problem:

  • Run a nested loop using two integer pointers i and j for 0 <= i < N and 0 <= j < M
  • Set B[i][j] equal to A[j][i]

Below is the implementation of the above approach:

C++




// C++ program to find
// transpose of a matrix
#include <bits/stdc++.h>
using namespace std;
#define N 4
  
// This function stores transpose
// of A[][] in B[][]
void transpose(int A[][N], int B[][N])
{
    int i, j;
    for (i = 0; i < N; i++)
        for (j = 0; j < N; j++)
            B[i][j] = A[j][i];
}
  
// Driver code
int main()
{
    int A[N][N] = { { 1, 1, 1, 1 },
                    { 2, 2, 2, 2 },
                    { 3, 3, 3, 3 },
                    { 4, 4, 4, 4 } };
  
    // Note dimensions of B[][]
    int B[N][N], i, j;
  
      // Function call
    transpose(A, B);
  
    cout << "Result matrix is \n";
    for (i = 0; i < N; i++) {
        for (j = 0; j < N; j++)
            cout << " " << B[i][j];
  
        cout << "\n";
    }
    return 0;
}
  
// This code is contributed by shivanisinghss2110


C




// C program to find
// transpose of a matrix
  
#include <stdio.h>
#define N 4
  
// This function stores transpose of A[][] in B[][]
void transpose(int A[][N], int B[][N])
{
    int i, j;
    for (i = 0; i < N; i++)
        for (j = 0; j < N; j++)
            B[i][j] = A[j][i];
}
  
// Driver code
int main()
{
    int A[N][N] = { { 1, 1, 1, 1 },
                    { 2, 2, 2, 2 },
                    { 3, 3, 3, 3 },
                    { 4, 4, 4, 4 } };
  
    // Note dimensions of B[][]
    int B[N][N], i, j;
  
      // Function call
    transpose(A, B);
  
    printf("Result matrix is \n");
    for (i = 0; i < N; i++) {
        for (j = 0; j < N; j++)
            printf("%d ", B[i][j]);
        printf("\n");
    }
  
    return 0;
}


Java




// Java Program to find
// transpose of a matrix
  
class GFG {
    static final int N = 4;
  
    // This function stores transpose
    // of A[][] in B[][]
    static void transpose(int A[][], int B[][])
    {
        int i, j;
        for (i = 0; i < N; i++)
            for (j = 0; j < N; j++)
                B[i][j] = A[j][i];
    }
  
    // Driver code
    public static void main(String[] args)
    {
        int A[][] = { { 1, 1, 1, 1 },
                      { 2, 2, 2, 2 },
                      { 3, 3, 3, 3 },
                      { 4, 4, 4, 4 } };
  
        int B[][] = new int[N][N], i, j;
  
          // Function call
        transpose(A, B);
  
        System.out.print("Result matrix is \n");
        for (i = 0; i < N; i++) {
            for (j = 0; j < N; j++)
                System.out.print(B[i][j] + " ");
            System.out.print("\n");
        }
    }
}
  
// This code is contributed by Anant Agarwal.


Python3




# Python3 Program to find
# transpose of a matrix
  
N = 4
  
# This function stores
# transpose of A[][] in B[][]
  
  
def transpose(A, B):
  
    for i in range(N):
        for j in range(N):
            B[i][j] = A[j][i]
  
  
# Driver code
if __name__ == '__main__':
  A = [[1, 1, 1, 1],
       [2, 2, 2, 2],
       [3, 3, 3, 3],
       [4, 4, 4, 4]]
  
  
  # To store result
  B = [[0 for x in range(N)] for y in range(N)]
  
  # Function call
  transpose(A, B)
  
  print("Result matrix is")
  for i in range(N):
      for j in range(N):
          print(B[i][j], " ", end='')
      print()


C#




// C# Program to find
// transpose of a matrix
using System;
class GFG {
  
    static int N = 4;
  
    // This function stores transpose
    // of A[][] in B[][]
    static void transpose(int[, ] A, int[, ] B)
    {
        int i, j;
        for (i = 0; i < N; i++)
            for (j = 0; j < N; j++)
                B[i, j] = A[j, i];
    }
  
    // Driver code
    public static void Main()
    {
        int[, ] A = { { 1, 1, 1, 1 },
                      { 2, 2, 2, 2 },
                      { 3, 3, 3, 3 },
                      { 4, 4, 4, 4 } };
  
        int[, ] B = new int[N, N];
  
          // Function call
        transpose(A, B);
  
        Console.WriteLine("Result matrix is \n");
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++)
                Console.Write(B[i, j] + " ");
            Console.Write("\n");
        }
    }
}
  
// This code is contributed by nitin mittal


Javascript




<script>
// javascript Program to find 
// transpose of a matrix    
  
     var N = 4;
  
    // This function stores transpose
    // of A in B
    function transpose(A , B) {
        var i, j;
        for (i = 0; i < N; i++)
            for (j = 0; j < N; j++)
                B[i][j] = A[j][i];
    }
  
    // Driver code
      
        var A = [ [ 1, 1, 1, 1 ], [ 2, 2, 2, 2 ], [ 3, 3, 3, 3 ], [4, 4, 4, 4]];
  
        var B = Array(N);
        for(i=0;i<N;i++)
        B[i] =Array(N).fill(0);
  
        transpose(A, B);
  
        document.write("Result matrix is <br/>");
        for (i = 0; i < N; i++) {
            for (j = 0; j < N; j++)
                document.write(B[i][j] + " ");
            document.write("<br/>");
        }
// This code contributed by Rajput-Ji
</script>


PHP




<?php
  
// This function stores transpose
// of A[][] in B[][]
function transpose(&$A, &$B)
{
    $N = 4;
    for ($i = 0; $i < $N; $i++)
        for ($j = 0; $j < $N; $j++)
            $B[$i][$j] = $A[$j][$i];
}
  
// Driver code
  
$A = array(array(1, 1, 1, 1),
           array(2, 2, 2, 2),
           array(3, 3, 3, 3),
           array(4, 4, 4, 4));
  
$N = 4;
  
// Function call
transpose($A, $B);
  
echo "Result matrix is \n";
for ($i = 0; $i < $N; $i++)
{
    for ($j = 0; $j < $N; $j++)
    {
    echo $B[$i][$j];
    echo " ";
    }
    echo "\n";
}
  
// This code is contributed
// by Shivi_Aggarwal 
?>


Output

Result matrix is 
 1 2 3 4
 1 2 3 4
 1 2 3 4
 1 2 3 4

Time complexity: O(M x N).
Auxiliary Space: O(M x N), since M x N extra space has been used.

Program to find the transpose of a matrix using constant space:

Note – This approach works only for square matrices (i.e., – where no. of rows are equal to the number of columns). This algorithm is also known as an “in-place” algorithm as it uses no extra space to solve the problem.

Follow the given steps to solve the problem:

  • Run a nested loop using two integer pointers i and j for 0 <= i < N and i+1 <= j < N
  • Swap A[i][j] with A[j][i]

Below is the implementation of the above approach:

C++




#include <bits/stdc++.h>
using namespace std;
  
#define N 4
  
// Converts A[][] to its transpose
void transpose(int A[][N])
{
    for (int i = 0; i < N; i++)
        for (int j = i + 1; j < N; j++)
            swap(A[i][j], A[j][i]);
}
  
int main()
{
    int A[N][N] = { { 1, 1, 1, 1 },
                    { 2, 2, 2, 2 },
                    { 3, 3, 3, 3 },
                    { 4, 4, 4, 4 } };
  
    transpose(A);
  
    printf("Modified matrix is \n");
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++)
            printf("%d ", A[i][j]);
        printf("\n");
    }
  
    return 0;
}


Java




// Java Program to find
// transpose of a matrix
  
class GFG {
    static final int N = 4;
  
    // Finds transpose of A[][] in-place
    static void transpose(int A[][])
    {
        for (int i = 0; i < N; i++)
            for (int j = i + 1; j < N; j++) {
                int temp = A[i][j];
                A[i][j] = A[j][i];
                A[j][i] = temp;
            }
    }
  
    // Driver code
    public static void main(String[] args)
    {
        int A[][] = { { 1, 1, 1, 1 },
                      { 2, 2, 2, 2 },
                      { 3, 3, 3, 3 },
                      { 4, 4, 4, 4 } };
  
        transpose(A);
  
        System.out.print("Modified matrix is \n");
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++)
                System.out.print(A[i][j] + " ");
            System.out.print("\n");
        }
    }
}


Python3




# Python3 Program to find
# transpose of a matrix
  
N = 4
  
# Finds transpose of A[][] in-place
  
  
def transpose(A):
  
    for i in range(N):
        for j in range(i+1, N):
            A[i][j], A[j][i] = A[j][i], A[i][j]
  
  
# driver code
A = [[1, 1, 1, 1],
     [2, 2, 2, 2],
     [3, 3, 3, 3],
     [4, 4, 4, 4]]
  
transpose(A)
  
print("Modified matrix is")
for i in range(N):
    for j in range(N):
        print(A[i][j], " ", end='')
    print()
  
# This code is contributed
# by Anant Agarwal.


C#




// C# Program to find transpose of
// a matrix
using System;
  
class GFG {
  
    static int N = 4;
  
    // Finds transpose of A[][] in-place
    static void transpose(int[, ] A)
    {
        for (int i = 0; i < N; i++)
            for (int j = i + 1; j < N; j++) {
                int temp = A[i, j];
                A[i, j] = A[j, i];
                A[j, i] = temp;
            }
    }
  
    // Driver code
    public static void Main()
    {
        int[, ] A = { { 1, 1, 1, 1 },
                      { 2, 2, 2, 2 },
                      { 3, 3, 3, 3 },
                      { 4, 4, 4, 4 } };
  
        transpose(A);
  
        Console.WriteLine("Modified matrix is ");
        for (int i = 0; i < N; i++) {
            for (int j = 0; j < N; j++)
                Console.Write(A[i, j] + " ");
  
            Console.WriteLine();
        }
    }
}
  
// This code is contributed by anuj_67.


Javascript




<script>
  
// JavaScript Program to find 
// transpose of a matrix    
var N = 4;
  
    // Finds transpose of A in-place
    function transpose(A) {
        for (i = 0; i < N; i++)
            for (j = i + 1; j < N; j++) {
                var temp = A[i][j];
                A[i][j] = A[j][i];
                A[j][i] = temp;
            }
    }
  
    // Driver code
      
        var A = [ [ 1, 1, 1, 1 ], 
                  [ 2, 2, 2, 2 ],
                  [ 3, 3, 3, 3 ], 
                  [ 4, 4, 4, 4 ] ];
  
        transpose(A);
  
        document.write("Modified matrix is <br/>");
        for (i = 0; i < N; i++) {
            for (j = 0; j < N; j++)
                document.write(A[i][j] + " ");
            document.write("\<br/>");
        }
  
// This code is contributed by aashish1995
  
</script>


PHP




<?php
// Converts A[][] to its transpose
function transpose(&$A)
{
    $N = 4;
    for ($i = 0; $i < $N; $i++)
        for ($j = $i + 1; $j < $N; $j++)
            {
                $temp = $A[$i][$j];
                $A[$i][$j] = $A[$j][$i];
                $A[$j][$i] = $temp;
            }
}
  
// Driver Code
$N = 4;
$A = array(array(1, 1, 1, 1),
            array(2, 2, 2, 2),
           array(3, 3, 3, 3),
           array(4, 4, 4, 4));
  
transpose($A);
  
echo "Modified matrix is " . "\n";
for ($i = 0; $i < $N; $i++)
{
    for ($j = 0; $j < $N; $j++)
        echo $A[$i][$j] . " ";
    echo "\n";
}
  
// This code is contributed
// by Akanksha Rai(Abby_akku)
?>


Output

Modified matrix is 
1 2 3 4 
1 2 3 4 
1 2 3 4 
1 2 3 4 

Time complexity: O(N2).
Auxiliary Space: O(1)



Previous Article
Next Article

Similar Reads

Inplace (Fixed space) M x N size matrix transpose
Given an M x N matrix, transpose the matrix without auxiliary memory.It is easy to transpose matrix using an auxiliary array. If the matrix is symmetric in size, we can transpose the matrix inplace by mirroring the 2D array across it's diagonal (try yourself). How to transpose an arbitrary size matrix inplace? See the following matrix, a b c a d g
15+ min read
Generate matrix from given Sparse Matrix using Linked List and reconstruct the Sparse Matrix
Given a sparse matrix mat[][] of dimensions N*M, the task is to construct and represent the original matrix using a Linked List and reconstruct the givensparse matrix. Examples: Input: mat[][] = {{0, 1, 0, 0, 0}, {0, 1, 0, 0, 0}, {0, 0, 2, 0, 0}, {0, 3, 0, 0, 4}, {0, 0, 5, 0, 0}}Output:Linked list representation: (4, 2, 5) ? (3, 4, 4) ? (3, 1, 3) ?
15+ min read
Generate a Matrix such that given Matrix elements are equal to Bitwise OR of all corresponding row and column elements of generated Matrix
Given a matrix B[][] of dimensions N * M, the task is to generate a matrix A[][] of same dimensions that can be formed such that for any element B[i][j] is equal to Bitwise OR of all elements in the ith row and jth column of A[][]. If no such matrix exists, print "Not Possible". Otherwise, print the matrix A[][]. Examples: Input: B[][] = {{1, 1, 1}
11 min read
Program to check if a matrix is Binary matrix or not
Given a matrix, the task is to check if that matrix is a Binary Matrix. A Binary Matrix is a matrix in which all the elements are either 0 or 1. It is also called Logical Matrix, Boolean Matrix, Relation Matrix. Examples: Input: {{1, 0, 1, 1}, {0, 1, 0, 1} {1, 1, 1, 0}} Output: Yes Input: {{1, 0, 1, 1}, {1, 2, 0, 1}, {0, 0, 1, 1}} Output: No Approa
5 min read
Program to convert given Matrix to a Diagonal Matrix
Given a N*N matrix. The task is to convert the matrix to a diagonal matrix. That is to change the values of the non-diagonal elements of a matrix to 0.Diagonal-Matrix: A matrix is called a Diagonal Matrix if all the non-diagonal elements of the matrix are zero.Examples: Input : mat[][] = {{ 2, 1, 7 }, { 3, 7, 2 }, { 5, 4, 9 }} Output : {{2, 0, 7},
8 min read
Program to check diagonal matrix and scalar matrix
Diagonal matrixA square matrix is said to be a diagonal matrix if the elements of the matrix except the main diagonal are zero. A square null matrix is also a diagonal matrix whose main diagonal elements are zero. Examples: Input: Mat[4][4] = {{4, 0, 0, 0}, {0, 5, 0, 0}, {0, 0, 2, 0}, {0, 0, 0, 1}}Output: YesInput:Mat[4][4] = {{6, 10, 12, 0}, {0, 5
10 min read
Find the original matrix from the given AND matrix
Given a binary matrix B[][] of size N*M, the task is to find a matrix A[][] of the same size such that B[i][j] is the bitwise AND of all the elements in ith row and jth column of A[][]. Examples: Input: B[][] = { {1, 0, 1}, {0, 0, 0} }Output: { {1, 1, 1}, {1, 0, 1} }Explanation:1 1 1 ? 1 0 11 0 1 0 0 0 Input: B[][] = { {0, 0}, {1, 1} }Output: -1 Ap
10 min read
Program to find all types of Matrix
Given the dimension of the matrix R(rows) * C(column) the task is to find which type of matrix is represented by the given dimension. Examples: Input : R = 1 C = 0Output : Row Matrix Input : R = 4 C = 5Output : Horizontal Matrix Row Matrix : When R = 1 and C = 0 then the matrix represent Row Matrix .Column Matrix : When C = 1 and R = 0 then the mat
5 min read
Program to find the maximum element in a Matrix
Given an NxM matrix. The task is to find the maximum element in this matrix. Examples: Input: mat[4][4] = {{1, 2, 3, 4}, {25, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 16}}; Output: 25 Input: mat[3][4] = {{9, 8, 7, 6}, {5, 4, 3, 2}, {1, 0, 12, 45}}; Output: 45 Approach: The idea is to traverse the matrix using two nested loops, one for rows and one f
6 min read
Program to find the Sum of each Row and each Column of a Matrix
Given a matrix of order m×n, the task is to find out the sum of each row and each column of a matrix. Examples: Input: array[4][4] = { {1, 1, 1, 1}, {2, 2, 2, 2}, {3, 3, 3, 3}, {4, 4, 4, 4}}; Output: Sum of the 0 row is = 4 Sum of the 1 row is = 8 Sum of the 2 row is = 12 Sum of the 3 row is = 16 Sum of the 0 column is = 10 Sum of the 1 column is =
9 min read
Program to find the Product of diagonal elements of a matrix
Given an N * N matrix, the task is to find the product of the elements of left and right diagonal. Examples: Input: arr[] = 1 2 3 4 5 6 7 8 9 7 4 2 2 2 2 1Output: 9408Explanation:Product of left diagonal = 1 * 4 * 6 * 1 = 24Product of right diagonal = 4 * 7 * 7 * 2 = 392Total product = 24 * 392 = 9408 Input: arr[] = 2 1 2 1 2 1 2 1 2 1 2 1 2 1 2 1
7 min read
Javascript Program to Find maximum element of each row in a matrix
Given a matrix, the task is to find the maximum element of each row.Examples: Input : [1, 2, 3] [1, 4, 9] [76, 34, 21] Output : 3 9 76 Input : [1, 2, 3, 21] [12, 1, 65, 9] [1, 56, 34, 2] Output : 21 65 56 Approach : Approach is very simple. The idea is to run the loop for no_of_rows. Check each element inside the row and find for the maximum elemen
2 min read
Javascript Program to Find median in row wise sorted matrix
We are given a row-wise sorted matrix of size r*c, we need to find the median of the matrix given. It is assumed that r*c is always odd.Examples:  Input : 1 3 5 2 6 9 3 6 9 Output : Median is 5 If we put all the values in a sorted array A[] = 1 2 3 3 5 6 6 9 9) Input: 1 3 4 2 5 6 7 8 9 Output: Median is 5Recommended: Please solve it on “PRACTICE ”
4 min read
Program to find Normal and Trace of a matrix
Given a 2D matrix, the task is to find Trace and Normal of matrix.Normal of a matrix is defined as square root of sum of squares of matrix elements.Trace of a n x n square matrix is sum of diagonal elements. Examples : Input : mat[][] = {{7, 8, 9}, {6, 1, 2}, {5, 4, 3}}; Output : Normal = 16 Trace = 11 Explanation : Normal = sqrt(7*7+ 8*8 + 9*9 + 6
6 min read
Check if it is possible to make the given matrix increasing matrix or not
Given a matrix of N X M of positive integers, the task is to find out whether it is possible to make the matrix increasing or not. Print the matrix constructed otherwise print -1. Matrix elements should be greater than zero. A matrix is said to be increasing matrix if: For each row, the elements are in the increasing order.For each column, the elem
10 min read
Check if matrix can be converted to another matrix by transposing square sub-matrices
Given two N X M matrices of integers. In an operation, we can transpose any square sub-matrix in matrix1. The task is to check if matrix1 can be converted to matrix2 with the given operation.Examples: Input: matrix1[][] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9}}, matrix2[][] = { {1, 4, 7}, {2, 5, 6}, {3, 8, 9}} Output: Yes Transpose the whole matrix and
10 min read
Minimum swaps needed to convert given Binary Matrix A to Binary Matrix B
Given two binary matrices, A[][] and B[][] of size N×M, the task is to find the minimum number of swaps of the elements of matrix A, needed to convert matrix A into matrix B. If it is impossible to do so then print "-1". Examples : Input: A[][] = {{1, 1, 0}, {0, 0, 1}, {0, 1, 0}}, B[][] = {{0, 0, 1}, {0, 1, 0}, {1, 1, 0}}Output: 3Explanation: One p
8 min read
Minimum number of steps to convert a given matrix into Upper Hessenberg matrix
Given a matrix of order NxN, Find the minimum number of steps to convert the given matrix into the Upper Hessenberg matrix. In each step, the only operation allowed is to decrease or increase any element value by 1.Examples: Input : N=3 1 2 8 1 3 4 2 3 4 Output :2 Decrease the element a[2][0] 2 times. Now the matrix is upper hessenbergInput : N=4 1
4 min read
Minimum steps required to convert the matrix into lower hessenberg matrix
Given a matrix of order NxN, Find the minimum number of steps to convert given matrix into Lower Hessenberg matrix. In each step, the only operation allowed is to decrease or increase any element value by 1.Examples: Input: mat[][] = { {1, 2, 8}, {1, 3, 4}, {2, 3, 4}} Output: 8 Decrease the element a[0][2] 8 times. Now the matrix is lower Hessenber
4 min read
Minimum number of steps to convert a given matrix into Diagonally Dominant Matrix
Given a matrix of order NxN, the task is to find the minimum number of steps to convert given matrix into Diagonally Dominant Matrix. In each step, the only operation allowed is to decrease or increase any element by 1.Examples: Input: mat[][] = {{3, 2, 4}, {1, 4, 4}, {2, 3, 4}} Output: 5 Sum of the absolute values of elements of row 1 except the d
6 min read
Maximize Matrix sum by replacing 0s such that Matrix remains sorted
Given a matrix mat[][] of dimension N*M which is sorted row-wise and column-wise, the task is to find the sum of the elements of the matrix by replacing all 0s in the matrix with any value such that the matrix remains sorted row-wise and column-wise. If it is not possible, then print "-1". Note: 0s will be only present in internal cells. i.e., neit
7 min read
Check if a given matrix can be converted to another given matrix by row and column exchanges
Given a matrix startMatrix and another matrix finalMatrix, the task is to check if startMatrix can be converted to finalMatrix by column exchanges and a row exchanges. Examples: Input: start[][] = {{1, 2, 3, 4}, {5, 6, 7, 8} {9, 10, 11, 12}, {13, 14, 15, 16}}final[][] = {{1, 4, 3, 2}, {5, 8, 7, 6}, {13, 16, 15, 14}, {9, 12, 11, 10}} Output: Yes Exp
9 min read
Create matrix whose sum of diagonals in each sub matrix is even
Given a number N, the task is to create a square matrix of size N*N with values in range [1, N*N], such that the sum of each diagonal of an even sub-square matrix is even. Examples: Input: N = 3Output: 1 2 3 4 5 6 7 8 9 Explanation:For each even sub-square matrix the sum of each diagonal is a even number.1 2 4 5 sum of each diagonal is 6 and 6 i.e
5 min read
Construct a square Matrix whose parity of diagonal sum is same as size of matrix
Given an integer N representing the size of the matrix, the task is to construct a square matrix N * N which have an element from 1 to N2 such that the parity of the sum of its diagonals is equal to the parity of integer N. Examples: Input: N = 4Output:1 2 3 4 8 7 6 59 10 11 1216 15 14 13Explanation:Sum of diagonal = 32 and 36 and integer N = 4, al
6 min read
Minimize count of adjacent row swaps to convert given Matrix to a Lower Triangular Matrix
Given a matrix, mat[][] of size N × N, the task is to minimize the count of adjacent row swaps to convert the given matrix to a lower triangular matrix. If it is not possible to convert the given matrix to a lower triangular matrix, then print -1. Note: A lower triangular matrix contains 0s at all the indices above the main diagonal. Examples: Inpu
9 min read
Count right angled triangles in a matrix having two of its sides parallel to sides of the matrix
Given a binary matrix arr[][] of dimensions N * M , the task is to count the number of right-angled triangles that can be formed by joining the cells containing the value 1 such that the triangles must have any two of its sides parallel to the sides of the rectangle. Examples: Input: arr[][] = {{0, 1, 0}, {1, 1, 1}, {0, 1, 0}}Output: 4Explanation:
8 min read
Construct a Matrix such that each cell consists of sum of adjacent elements of respective cells in given Matrix
Given a matrix arr[][] of dimensions N * M, the task is to generate a matrix such that any cell (r, c) stores the sum of adjacent elements present horizontally, vertically, and diagonally in the given matrix. Examples: Input: arr[][] = {{1, 3}, {2, 4}}Output: {{9, 7}, {8, 6}}Explanation: Matrix is constructed by the following operations: For cell (
9 min read
Print Matrix after multiplying Matrix elements N times
Given a square matrix mat[][] and an integer N, the task is to print the matrix after multiplying the matrix N times. Examples: Input: mat[][] = {{1, 2, 3}, {3, 4, 5}, {6, 7, 9}}, N = 2Output:25 31 4045 57 7481 103 134 Input: mat[][] = {{1, 2}, {3, 4}}, N = 3Output:37 5481 118 Approach: The idea is to use the Matrix Multiplication identity matrix.
9 min read
Convert a Matrix into another Matrix of given dimensions
Given a matrix, mat[][] of size N * M and two positive integers A and B, the task is to construct a matrix of size A * B from the given matrix elements. If multiple solutions exist, then print any one of them. Otherwise, print -1. Input: mat[][] = { { 1, 2, 3, 4, 5, 6 } }, A = 2, B = 3 Output: { { 1, 2, 3 }, { 4, 5, 6 } } Explanation: Since the siz
6 min read
Check if a Matrix can be superimposed on the given Matrix
Given a matrix letter[][] of size N * M, composed of '#' and '*' and another matrix stamp[][] of size X * Y containing only '$'. The task is to find if all the '*' of the larger one can be replaced by '$' by superimposing the stamp matrix on the letter matrix. Note: In a superimpose operation only a area having all the characters as '*' or '$' can
10 min read