C Programming – HackerRank Solution | ORTHOGONAL |

(HackerRank) Write a Modular C Programming code to solve ORTHOGONAL, Given a matrix check if the matrix is orthogonal or not. A matrix is orthogonal if A* AT = AT * A = I.

Given a matrix check if the matrix is orthogonal or not.

A matrix is orthogonal if A* AT = AT * A = I.

Where AT is transpose of A and I is the identity matrix

Input Format

First line contains order of matrix R and C.

Second line conatins R*C matrix elements separated by a space

Constraints

1<=R,C<=100

Output Format

Output YES if the matrix is orthogonal else output NO

Sample Input 0

5 0

Sample Output 0

Invalid

Sample Input 1

0 0

Sample Output 1

Invalid

Sample Input 2

2 2
1 0
0 1

Sample Output 2

YES

Explanation 2

Given matrix is orthogonal matrix

Refer : C Programming HackerRank all solutions for Loops | Arrays | strings | Data Structures | Linked lists | Stacks | Queues | Binary Trees

 

CODE:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>

int main() {
    int m,n,i,j,k;
    scanf("%d%d",&m,&n);
    if(m>0 && n>0)
    {
    int A[m][n];
    int AT[n][m];
    for(i=0;i<m;i++)
    {
      for(j=0;j<n;j++)
        {
          scanf("%d",&A[i][j]);
              AT[j][i]=A[i][j];
        }
    }
    int prod[m][m];
    for(i=0;i<m;i++)
    {
        for(j=0;j<m;j++)
        {
            prod[i][j]=0;
            for(k=0;k<n;k++)
            {
              prod[i][j]+=A[i][k]*AT[k][j];
            }
        }
    }
    int I[m][m];
    for(i=0;i<m;i++)
    {
        for(j=0;j<m;j++)
        {
            if(i==j)
                I[i][j]=1;
            else
                I[i][j]=0;
        }
     }
    int status=1;
    for(i=0;i<m;i++)
    {
        for(j=0;j<m;j++)
        {
            if(prod[i][j]!=I[i][j])
            {  
                status=0;
            }
        }
    }
    if(status==1)
        printf("YES");
    else
        printf("NO");
}
    else{
        printf("Invalid");
    }
    return 0;
}

OUTPUT

Congratulations, you passed the sample test case.

Click the Submit Code button to run your code against all the test cases.

Input (stdin)

5 0
Your Output (stdout)

Invalid
Expected Output

Invalid

 

Please find some more codes of Loops, Condition Statements, 1D Arrays, 2D Arrays, Strings, Pointers, Data Structures, Files, Linked lists, Stacks, Queues, Binary Trees, MISC, Solved model question papers & Hacker Rank all solutions on the below page:

Top 100+ C Programming codes – KLE Technological University

Leave a Comment

Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

Welcome to FactsPrime

Sorry, We have detected that you have activated Ad-Blocker. Please Consider supporting us by disabling your Ad Blocker, It helps us in maintaining this website. To View the content, Please disable adblocker and refresh the page.

Thank You !!!