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 Input1
4
1 2 3 4
2 3 4 1
3 4 1 2
4 1 2 3
Sample Output1
True
Sample Input2
4
1 2 3 3
2 3 4 1
3 4 1 2
4 1 2 3
Sample Output2
False
arr = [ [1,2,3,4,5],
[3,4,2,5,1],
[2,1,5,3,4],
[4,5,1,2,3],
[5,3,4,1,2]];
dict = {}
for i in range(len(arr)):
for j in range(len(arr[i])):
if dict.get(arr[i][j]) is None:
dict[arr[i][j]] = 1
else:
dict[arr[i][j]] = dict.get(arr[i][j]) + 1
is_unique = True
for key in dict:
if dict.get(key) != len(arr) or key > len(arr) or key < 1:
print(key)
print(dict.get(key))
is_unique = False
print(is_unique)
Comments
Leave a comment