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.
Output: True Explanation: Given matrix if multiplied by itself returns the same 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.
C++
// Program to check given matrix // is idempotent matrix or not.#include<bits/stdc++.h>usingnamespacestd;// Function for matrix multiplication.voidmulMatrix(vector<vector<int>>&mat,vector<vector<int>>&res){intn=mat.size();for(inti=0;i<n;i++){for(intj=0;j<n;j++){for(intk=0;k<n;k++){res[i][j]+=mat[i][k]*mat[k][j];}}}}// Function to check idempotent// property of matrix.boolcheckIdempotent(vector<vector<int>>&mat){intn=mat.size();// multiply matrix mat by iself// and store it into res.vector<vector<int>>res(n,vector<int>(n,0));mulMatrix(mat,res);// check if all the elements of // res are same as mat.for(inti=0;i<n;i++)for(intj=0;j<n;j++)if(mat[i][j]!=res[i][j])returnfalse;returntrue;}intmain(){vector<vector<int>>mat={{2,-2,-4},{-1,3,4},{1,-2,-3}};if(checkIdempotent(mat))cout<<"True";elsecout<<"False";return0;}
Java
// Java program to check given matrix // is idempotent matrix or not.importjava.io.*;classGfG{// Function for matrix multiplication.staticvoidmulMatrix(int[][]mat,int[][]res){intn=mat.length;// Perform matrix multiplicationfor(inti=0;i<n;i++){for(intj=0;j<n;j++){res[i][j]=0;for(intk=0;k<n;k++){res[i][j]+=mat[i][k]*mat[k][j];}}}}// Function to check idempotent// property of matrix.staticbooleancheckIdempotent(intmat[][]){intn=mat.length;// Calculate multiplication of matrix// with itself and store it into res.intres[][]=newint[n][n];mulMatrix(mat,res);for(inti=0;i<n;i++){for(intj=0;j<n;j++){if(mat[i][j]!=res[i][j])returnfalse;}}returntrue;}publicstaticvoidmain(String[]args){intmat[][]={{2,-2,-4},{-1,3,4},{1,-2,-3}};if(checkIdempotent(mat))System.out.println("True");elseSystem.out.println("False");}}
Python
# Python Program to check given matrix # is idempotent matrix or not.importmath# Function for matrix multiplication.defmulMatrix(mat,res):n=len(mat)# Perform matrix multiplicationforiinrange(n):forjinrange(n):forkinrange(n):res[i][j]+=mat[i][k]*mat[k][j]# Function to check idempotent# property of matrix.defcheckIdempotent(mat):n=len(mat)# Calculate multiplication of matrix# with itself and store it into res.res=[[0]*nforiinrange(0,n)]mulMatrix(mat,res)foriinrange(0,n):forjinrange(0,n):if(mat[i][j]!=res[i][j]):returnFalsereturnTruemat=[[2,-2,-4],[-1,3,4],[1,-2,-3]]if(checkIdempotent(mat)):print("True")else:print("False")
C#
// C# program to check given matrix // is idempotent matrix or not.usingSystem;classGfG{// Function for matrix multiplication.staticvoidmulMatrix(int[,]mat,int[,]res){intn=mat.GetLength(0);// Perform matrix multiplicationfor(inti=0;i<n;i++){for(intj=0;j<n;j++){res[i,j]=0;for(intk=0;k<n;k++){res[i,j]+=mat[i,k]*mat[k,j];}}}}// Function to check idempotent// property of matrix.staticboolcheckIdempotent(int[,]mat){intn=mat.GetLength(0);// Calculate multiplication of matrix// with itself and store it into res.int[,]res=newint[n,n];mulMatrix(mat,res);for(inti=0;i<n;i++){for(intj=0;j<n;j++){if(mat[i,j]!=res[i,j])returnfalse;}}returntrue;}staticvoidMain(){int[,]mat={{2,-2,-4},{-1,3,4},{1,-2,-3}};if(checkIdempotent(mat))Console.WriteLine("True");elseConsole.WriteLine("False");}}
JavaScript
// Javascript program to check given matrix // is idempotent matrix or not.// Function for matrix multiplication.functionmulMatrix(mat,res){constn=mat.length;// Perform matrix multiplicationfor(leti=0;i<n;i++){for(letj=0;j<n;j++){for(letk=0;k<n;k++){res[i][j]+=mat[i][k]*mat[k][j];}}}}// Function to check idempotent// property of matrix.functioncheckIdempotent(mat){constn=mat.length;// Calculate multiplication of matrix// with itself and store it into res.constres=Array.from(Array(n),()=>Array(n).fill(0));mulMatrix(mat,res);for(leti=0;i<n;i++){for(letj=0;j<n;j++){if(mat[i][j]!=res[i][j])returnfalse;}}returntrue;}//driver codeconstmat=[[2,-2,-4],[-1,3,4],[1,-2,-3]];if(checkIdempotent(mat))console.log("True");elseconsole.log("False");
Output
True
Time Complexity: O(n3) Auxiliary Space: O(n2), since n2 extra space has been taken to store the result of multiplication.