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.
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.