using
System;
using
System.Collections.Generic;
class
GFG{
static
void
CheckLatinSquare(
int
[, ] mat)
{
int
N = mat.GetLength(0);
HashSet<
int
>[] rows =
new
HashSet<
int
>[ N ];
HashSet<
int
>[] cols =
new
HashSet<
int
>[ N ];
for
(
int
i = 0; i < N; i++)
{
rows[i] =
new
HashSet<
int
>();
cols[i] =
new
HashSet<
int
>();
}
int
invalid = 0;
for
(
int
i = 0; i < N; i++)
{
for
(
int
j = 0; j < N; j++)
{
rows[i].Add(mat[i, j]);
cols[j].Add(mat[i, j]);
if
(mat[i, j] > N || mat[i, j] <= 0)
{
invalid++;
}
}
}
int
numrows = 0;
int
numcols = 0;
for
(
int
i = 0; i < N; i++)
{
if
(rows[i].Count != N)
{
numrows++;
}
if
(cols[i].Count != N)
{
numcols++;
}
}
if
(numcols == 0 && numrows == 0 && invalid == 0)
Console.Write(
"YES"
+
"\n"
);
else
Console.Write(
"NO"
+
"\n"
);
return
;
}
public
static
void
Main(String[] args)
{
int
[, ] Matrix = {{1, 2, 3, 4},
{2, 1, 4, 3},
{3, 4, 1, 2},
{4, 3, 2, 1}};
CheckLatinSquare(Matrix);
}
}