#K52047. Valid Latin Square Matrix
Valid Latin Square Matrix
Valid Latin Square Matrix
You are given an n x n matrix. A matrix is called a Latin square if every row and every column contains exactly k distinct elements, precisely the numbers from 1 to k with no repetitions.
In other words, for each row and each column, if we sort the elements, we should get the sequence \(1, 2, \ldots, k\). Your task is to determine whether the provided matrix is valid in this sense.
Input Format: The first line contains two integers \(n\) and \(k\). Each of the following \(n\) lines contains \(n\) integers representing a row of the matrix.
Output Format: Print YES
if the matrix is valid, otherwise print NO
.
inputFormat
The input is read from stdin and has the following format:
n k row_1_element_1 row_1_element_2 ... row_1_element_n row_2_element_1 row_2_element_2 ... row_2_element_n ... row_n_element_1 row_n_element_2 ... row_n_element_n
Here, the first line contains two integers \(n\) and \(k\) separated by space. The next \(n\) lines each contain \(n\) integers, representing the rows of the matrix.
outputFormat
Output to stdout a single line containing either YES
if the matrix is valid, or NO
otherwise.
4 4
1 2 3 4
2 3 4 1
3 4 1 2
4 1 2 3
YES