Sunday, November 23, 2025
HomeLanguagesJavaJava Program to check idempotent matrix

Java Program to check idempotent matrix

Given a N * N matrix and the task is to check matrix is idempotent matrix or not.
Idempotent matrix: A matrix is said to be idempotent matrix if matrix multiplied by itself return the same matrix. The matrix M is said to be idempotent matrix if and only if M * M = M. In idempotent matrix M is a square matrix.
 

idempotent matrix

Examples: 
 

Input : mat[][] = {{3, -6},
                   {1, -2}};
Output : Idempotent Matrix

Input : mat[N][N] = {{2, -2, -4},
                     {-1, 3, 4},
                     {1, -2, -3}}
Output : Idempotent Matrix.

 

Java




// Java program to check given matrix
// is idempotent matrix or not.
import java.io.*;
 
class GFG
{
    static int N = 3;
     
    // Function for matrix multiplication.
    static void multiply(int mat[][], int res[][])
    {
        for (int i = 0; i < N; i++)
        {
            for (int j = 0; j < N; j++)
            {
                res[i][j] = 0;
                for (int k = 0; k < N; k++)
                    res[i][j] += mat[i][k] * mat[k][j];
            }
        }
    }
     
    // Function to check idempotent
    // property of matrix.
    static boolean checkIdempotent(int mat[][])
    {
        // Calculate multiplication of matrix
        // with itself and store it into res.
        int res[][] = new int[N][N];
        multiply(mat, res);
     
        for (int i = 0; i < N; i++)
        {
            for (int j = 0; j < N; j++)
            {
                if (mat[i][j] != res[i][j])
                    return false;
            }
        }
        return true;
    }
 
    // Driver code.
    public static void main (String[] args)
    {
        int mat[][] = {{2, -2, -4},
                       {-1, 3, 4},
                       {1, -2, -3}};
     
        // checkIdempotent function call.
        if (checkIdempotent(mat))
            System.out.println( "Idempotent Matrix");
        else
            System.out.println("Not Idempotent Matrix.");
         
    }
}
 
// This code is contributed by vt_m.


Output:

Idempotent Matrix

Time complexity: O(N3)

Auxiliary space: O(N2)

Please refer complete article on Program to check idempotent matrix for more details!

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32407 POSTS0 COMMENTS
Milvus
97 POSTS0 COMMENTS
Nango Kala
6785 POSTS0 COMMENTS
Nicole Veronica
11932 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12000 POSTS0 COMMENTS
Shaida Kate Naidoo
6907 POSTS0 COMMENTS
Ted Musemwa
7168 POSTS0 COMMENTS
Thapelo Manthata
6864 POSTS0 COMMENTS
Umr Jansen
6852 POSTS0 COMMENTS