VOOZH about

URL: https://www.geeksforgeeks.org/dsa/program-check-idempotent-matrix/

⇱ Program to check idempotent matrix - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Program to check idempotent matrix

Last Updated : 23 Dec, 2024

Given a square matrix mat[][] of order n*n, the task is to check if it is an Idempotent Matrix or not.

Idempotent matrix: A matrix is said to be an idempotent matrix if the matrix multiplied by itself returns the samematrix, i.e. the matrix mat[][] is said to be an idempotent matrix if and only if M * M = M.

Examples:

Input: mat[][] = [[2, -2, -4],
[-1, 3, 4],
[1, -2, 3]]

Output: True
Explanation: Given matrix if multiplied by itself returns the same matrix.

👁 Idempotent-Matrix


Input: mat[][] = [[1, 2],
[3, 4]]
Output: False
Explanation: Given matrix if multiplied by itself returns the different matrix.

Approach:

The idea is to multiply matrix mat[][] by itself and check if all the elements of resultant matrix res[][] is equal to corresponding elements of mat[][]. Firstly multiply matrix mat[][] by itself using Matrix Multiplication and store the result in matrix res[][]. Then compare if both the matrices are equal or not.


Output
True

Time Complexity: O(n3)
Auxiliary Space: O(n2), since n2 extra space has been taken to store the result of multiplication.

Comment
Article Tags:
Article Tags: