VOOZH about

URL: https://www.geeksforgeeks.org/dsa/print-matrix-after-multiplying-matrix-elements-n-times/

⇱ Print Matrix after multiplying Matrix elements N times - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Print Matrix after multiplying Matrix elements N times

Last Updated : 23 Jul, 2025

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 = 2
Output:
25 31 40
45 57 74
81 103 134

Input: mat[][] = {{1, 2}, {3, 4}}, N = 3
Output:
37 54
81 118

Approach: The idea is to use the Matrix Multiplication identity matrix. i.e., A = IA and A = AI, where A is a matrix of N * M order dimensions and I is the identity matrix of dimensions M * N, where N is the total number of rows and M is the total number of columns in a matrix.

The idea is to iterate over the range [1, N] and update the Identity Matrix with A.I so that after calculating the value of A2 = A.A, A3 can be calculated as A.A2 and so on till AN.

Below is the implementation of the above approach:


Output: 
25 31 40 
45 57 74 
81 103 134

 

Time Complexity: O(N3)
Auxiliary Space: O(N)

Comment