Matrix - Identical Row and Column
Matrix - Identical Row and Column
PROBLEM STATEMENT :
The program must accept an integer matrix of size N*N as the input. The program must print YES if at least one row is identical to one column in the given matrix. Else the program must print NO as the output.
Boundary Condition(s):
1 <= Matrix element value <= 1000
Input Format:
The next N lines, each contain N integers separated by a space.
Output Format:
The first line contains YES or NO.
Explanation: Example Input/Output 3: Input: Output: NO
Example Input/Output 1:
Input: ()
4
2 3 4 5
3 5 10 9
6 10 8 8
7 9 2 1
Output:
YES
Explanation:
2nd row and 2nd column of the given matrix are identical.
3 5 10 9
6 10 8 8
7 9 2 1
So YES is printed as the output.
Example Input/Output 2:
Input: ()
3
4 5 3
1 3 3
4 5 1
Output:
YES
Explanation:
2nd row and 3rd column(bottom to top) are identical.
1 3 3
4 5 1
So YES is printed as the output.
Example Input/Output 3:
Input: ()
5
94 54 52 97 81
54 87 92 35 72
52 35 94 63 23
97 35 87 63 76
80 72 18 72 10
Output:
NO
1) LEARN THRICE
👇
2) THINK TWICE
👇
3) APPLY ONCE
SOLUTION :
C (Programming Language)
#include<stdio.h>
#include<stdlib.h>
int main()
{
int N;
scanf("%d",&N);
int mat[N][N];
for(int i=0;i<N;i++)
{
for(int j=0;j<N;j++) scanf("%d",&mat[i][j]);
}
for(int i=0;i<N;i++)
{
for(int j=0;j<N;j++)
{
if(mat[i][0]==mat[0][j])
{
int flag=0;
for(int i1=0;i1<N;i1++)
{
if(mat[i][i1]!=mat[i1][j]) flag=1;
}
if(flag==0) printf("YES"),exit(0);
}
if(mat[i][0]==mat[N-1][j])
{
int flag=0;
for(int i1=0;i1<N;i1++)
{
if(mat[i][i1]!=mat[N-1-i1][j]) flag=1;
}
if(flag==0) printf("YES"),exit(0);
}
}
}
printf("NO");
}
JAVA
import java.util.*;
public class Hello {
public static void main(String[] args) {
//Your Code Here
Scanner sc=new Scanner(System.in);
int size=sc.nextInt();
int mat[][]=new int[size][size];
for(int i=0;i<size;i++){
for(int j=0;j<size;j++){
mat[i][j]=sc.nextInt();
}
}
for(int i=0;i<size;i++){
String temp1="",rtemp1="";
for(int j=0;j<size;j++){
temp1=temp1+mat[i][j];
rtemp1=mat[i][j]+rtemp1;
}
for(int k=0;k<size;k++){
String temp2="",rtemp2="";
for(int l=0;l<size;l++){
rtemp2=mat[l][k]+rtemp2;
temp2=temp2+mat[l][k];
}
if(temp1.contains(temp2)||temp1.contains(rtemp2)||rtemp1.contains(temp2)||rtemp1.contains(rtemp2)){
System.out.print("YES");
System.exit(0);
}
}
}
System.out.print("NO");
}
}
PYTHON
n=int(input())
mat=[list(map(int,input().split())) for i in range(n)]
mattp=[[mat[i][j] for i in range(n) ] for j in range(n)]
for i in mat:
if i in mattp or i[::-1] in mattp:
print("YES")
exit()
print("NO")

Comments
Post a Comment