Problem Statement:
You are given a matrix, you need to check if it is orthogonal or not.
A matrix is considered as orthogonal, when we multiply the orthogonal to its transpose, we get the identity matrix.
Example:
Input:
1 0 0
0 1 0
0 0 1
Output:
Yes
Solution Explanation:
Solution is very simple.
First find the transpose of matrix.
Then multiply the transpose with the given matrix.
Then check if the matrix is identity or not.
Time Complexity: O(n*n*n)
Space Complexity: O(n*n)
Code Solution
#include <iostream>
#include <vector>
using namespace std;
const int MAX = 100;
bool solution(int a[][MAX], int m, int n)
{
if (m != n)
return false;
int prod[n][n];
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
int sum = 0;
for (int k = 0; k < n; k++)
{
sum = sum + (a[i][k] * a[j][k]);
}
prod[i][j] = sum;
}
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (i != j && prod[i][j] != 0)
return false;
if (i == j && prod[i][j] != 1)
return false;
}
}
return true;
}
int main()
{
int mat[][MAX] = {{1, 0, 0},
{0, 1, 0},
{0, 0, 1}};
if (solution(mat, 3, 3))
cout << "Yes";
else
cout << "No";
return 0;
}
Output
Yes