Program to check Involutory Matrix
Last Updated :
19 Aug, 2022
Given a matrix and the task is to check matrix is involutory matrix or not.
Involutory Matrix: A matrix is said to be involutory matrix if matrix multiply by itself return the identity matrix. Involutory matrix is the matrix that is its own inverse. The matrix A is said to be involutory matrix if A * A = I. Where I is the identity matrix.

Examples:
Input : mat[N][N] = {{1, 0, 0},
{0, -1, 0},
{0, 0, -1}}
Output : Involutory Matrix
Input : mat[N][N] = {{1, 0, 0},
{0, 1, 0},
{0, 0, 1}}
Output : Involutory Matrix
Implementation:
C++
// Program to implement involutory matrix.
#include <bits/stdc++.h>
#define N 3
using namespace std;
// Function for matrix multiplication.
void multiply(int mat[][N], int res[][N])
{
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 involutory matrix.
bool InvolutoryMatrix(int mat[N][N])
{
int res[N][N];
// multiply function call.
multiply(mat, res);
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (i == j && res[i][j] != 1)
return false;
if (i != j && res[i][j] != 0)
return false;
}
}
return true;
}
// Driver function.
int main()
{
int mat[N][N] = { { 1, 0, 0 },
{ 0, -1, 0 },
{ 0, 0, -1 } };
// Function call. If function return
// true then if part will execute otherwise
// else part will execute.
if (InvolutoryMatrix(mat))
cout << "Involutory Matrix";
else
cout << "Not Involutory Matrix";
return 0;
}
Java
// Java Program to implement
// involutory matrix.
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 involutory matrix.
static boolean InvolutoryMatrix(int mat[][])
{
int res[][] = new int[N][N];
// multiply function call.
multiply(mat, res);
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (i == j && res[i][j] != 1)
return false;
if (i != j && res[i][j] != 0)
return false;
}
}
return true;
}
// Driver function.
public static void main (String[] args)
{
int mat[][] = { { 1, 0, 0 },
{ 0, -1, 0 },
{ 0, 0, -1 } };
// Function call. If function return
// true then if part will execute
// otherwise else part will execute.
if (InvolutoryMatrix(mat))
System.out.println ( "Involutory Matrix");
else
System.out.println ( "Not Involutory Matrix");
}
}
// This code is contributed by vt_m
Python3
# Program to implement involutory matrix.
N = 3;
# Function for matrix multiplication.
def multiply(mat, res):
for i in range(N):
for j in range(N):
res[i][j] = 0;
for k in range(N):
res[i][j] += mat[i][k] * mat[k][j];
return res;
# Function to check involutory matrix.
def InvolutoryMatrix(mat):
res=[[0 for i in range(N)]
for j in range(N)];
# multiply function call.
res = multiply(mat, res);
for i in range(N):
for j in range(N):
if (i == j and res[i][j] != 1):
return False;
if (i != j and res[i][j] != 0):
return False;
return True;
# Driver Code
mat = [[1, 0, 0], [0, -1, 0], [0, 0, -1]];
# Function call. If function
# return true then if part
# will execute otherwise
# else part will execute.
if (InvolutoryMatrix(mat)):
print("Involutory Matrix");
else:
print("Not Involutory Matrix");
# This code is contributed by mits
C#
// C# Program to implement
// involutory matrix.
using System;
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 involutory matrix.
static bool InvolutoryMatrix(int [,]mat)
{
int [,]res = new int[N,N];
// multiply function call.
multiply(mat, res);
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (i == j && res[i,j] != 1)
return false;
if (i != j && res[i,j] != 0)
return false;
}
}
return true;
}
// Driver function.
public static void Main ()
{
int [,]mat = { { 1, 0, 0 },
{ 0, -1, 0 },
{ 0, 0, -1 } };
// Function call. If function return
// true then if part will execute
// otherwise else part will execute.
if (InvolutoryMatrix(mat))
Console.WriteLine( "Involutory Matrix");
else
Console.WriteLine( "Not Involutory Matrix");
}
}
// This code is contributed by vt_m
PHP
<?php
// Program to implement
// involutory matrix.
$N = 3;
// Function for matrix
// multiplication.
function multiply($mat, $res)
{
global $N;
for ($i = 0; $i < $N; $i++)
{
for ($j = 0; $j < $N; $j++)
{
$res[$i][$j] = 0;
for ($k = 0; $k < $N; $k++)
$res[$i][$j] += $mat[$i][$k] *
$mat[$k][$j];
}
}
return $res;
}
// Function to check
// involutory matrix.
function InvolutoryMatrix($mat)
{
global $N;
$res;
for ($i = 0; $i < $N; $i++)
for ($j = 0; $j < $N; $j++)
$res[$i][$j] = 0;
// multiply function call.
$res = multiply($mat, $res);
for ($i = 0; $i < $N; $i++)
{
for ($j = 0; $j < $N; $j++)
{
if ($i == $j &&
$res[$i][$j] != 1)
return false;
if ($i != $j &&
$res[$i][$j] != 0)
return false;
}
}
return true;
}
// Driver Code
$mat = array(array(1, 0, 0),
array(0, -1, 0),
array(0, 0, -1));
// Function call. If function
// return true then if part
// will execute otherwise
// else part will execute.
if (InvolutoryMatrix($mat))
echo "Involutory Matrix";
else
echo "Not Involutory Matrix";
// This code is contributed by mits
?>
JavaScript
<script>
// Javascript to implement involutory matrix.
var N = 3;
// Function for matrix multiplication.
function multiply(mat, res)
{
for(var i = 0; i < N; i++)
{
for(var j = 0; j < N; j++)
{
res[i][j] = 0;
for(var k = 0; k < N; k++)
res[i][j] += mat[i][k] * mat[k][j];
}
}
}
// Function to check involutory matrix.
function InvolutoryMatrix(mat)
{
var res = Array(N).fill(0).map(
x => Array(N).fill(0));
// Multiply function call.
multiply(mat, res);
for(var i = 0; i < N; i++)
{
for(var j = 0; j < N; j++)
{
if (i == j && res[i][j] != 1)
return false;
if (i != j && res[i][j] != 0)
return false;
}
}
return true;
}
// Driver code
var mat = [ [ 1, 0, 0 ],
[ 0, -1, 0 ],
[ 0, 0, -1 ] ];
// Function call. If function return
// true then if part will execute
// otherwise else part will execute.
if (InvolutoryMatrix(mat))
document.write("Involutory Matrix");
else
document.write("Not Involutory Matrix");
// This code is contributed by 29AjayKumar
</script>
Time Complexity: O(n3)
Auxiliary Space: O(n2), since n2 extra space has been taken.
Similar Reads
DSA Tutorial - Learn Data Structures and Algorithms DSA (Data Structures and Algorithms) is the study of organizing data efficiently using data structures like arrays, stacks, and trees, paired with step-by-step procedures (or algorithms) to solve problems effectively. Data structures manage how data is stored and accessed, while algorithms focus on
7 min read
Quick Sort QuickSort is a sorting algorithm based on the Divide and Conquer that picks an element as a pivot and partitions the given array around the picked pivot by placing the pivot in its correct position in the sorted array. It works on the principle of divide and conquer, breaking down the problem into s
12 min read
Merge Sort - Data Structure and Algorithms Tutorials Merge sort is a popular sorting algorithm known for its efficiency and stability. It follows the divide-and-conquer approach. It works by recursively dividing the input array into two halves, recursively sorting the two halves and finally merging them back together to obtain the sorted array. Merge
14 min read
SQL Commands | DDL, DQL, DML, DCL and TCL Commands SQL commands are crucial for managing databases effectively. These commands are divided into categories such as Data Definition Language (DDL), Data Manipulation Language (DML), Data Control Language (DCL), Data Query Language (DQL), and Transaction Control Language (TCL). In this article, we will e
7 min read
Data Structures Tutorial Data structures are the fundamental building blocks of computer programming. They define how data is organized, stored, and manipulated within a program. Understanding data structures is very important for developing efficient and effective algorithms. What is Data Structure?A data structure is a st
2 min read
Bubble Sort Algorithm Bubble Sort is the simplest sorting algorithm that works by repeatedly swapping the adjacent elements if they are in the wrong order. This algorithm is not suitable for large data sets as its average and worst-case time complexity are quite high.We sort the array using multiple passes. After the fir
8 min read
Breadth First Search or BFS for a Graph Given a undirected graph represented by an adjacency list adj, where each adj[i] represents the list of vertices connected to vertex i. Perform a Breadth First Search (BFS) traversal starting from vertex 0, visiting vertices from left to right according to the adjacency list, and return a list conta
15+ min read
Binary Search Algorithm - Iterative and Recursive Implementation Binary Search Algorithm is a searching algorithm used in a sorted array by repeatedly dividing the search interval in half. The idea of binary search is to use the information that the array is sorted and reduce the time complexity to O(log N). Binary Search AlgorithmConditions to apply Binary Searc
15 min read
Insertion Sort Algorithm Insertion sort is a simple sorting algorithm that works by iteratively inserting each element of an unsorted list into its correct position in a sorted portion of the list. It is like sorting playing cards in your hands. You split the cards into two groups: the sorted cards and the unsorted cards. T
9 min read
Array Data Structure Guide In this article, we introduce array, implementation in different popular languages, its basic operations and commonly seen problems / interview questions. An array stores items (in case of C/C++ and Java Primitive Arrays) or their references (in case of Python, JS, Java Non-Primitive) at contiguous
4 min read