Unique Matrix
You are given a N*N matrix. write a program to check if the matrix is unique or not. A unique Matrix is a matrix if every row and column of the matrix contains all the integers from 1 to N
Input
The first line contains an integer N.
The next N lines contains N space separated values of the matrix.
Output:
The output contains a single line and should be True if the matrix is unique matrix and False otherwise.
Sample input:
4
1 2 3 4
2 3 4 1
3 4 1 2
4 1 2 3
Sample output:
True
Sample Input2
4
1 2 3 3
2 3 4 1
3 4 1 2
4 1 2 3
Sample Output
False
n = int(input())
matrix = [[int(i) for i in input().split()] for _ in range(n)]
columns = [[matrix[i][j] for i in range(n)] for j in range(n)]
numbers = list(range(1, n + 1))
result = True
for i in range(n):
for j in range(n):
if numbers[j] not in matrix[i] or numbers[j] not in columns[i]:
result = False
print(result)
Comments
Leave a comment