using
System;
class
GFG
{
public
static
void
sortRows(
int
[][] mat,
int
n)
{
for
(
int
i = 0; i < n; i++)
{
Array.Sort(mat[i]);
}
}
public
static
void
findAndPrintCommonElements(
int
[][] mat,
int
n)
{
sortRows(mat, n);
int
[] curr_index =
new
int
[n];
int
f = 0;
for
(; curr_index[0] < n; curr_index[0]++)
{
int
value = mat[0][curr_index[0]];
bool
present =
true
;
for
(
int
i = 1; i < n; i++)
{
while
(curr_index[i] < n &&
mat[i][curr_index[i]] <= value)
{
curr_index[i]++;
}
if
(mat[i][curr_index[i] - 1] != value)
{
present =
false
;
}
if
(curr_index[i] == n)
{
f = 1;
break
;
}
}
if
(present)
{
Console.Write(value +
" "
);
}
if
(f == 1)
{
break
;
}
}
}
public
static
void
Main(
string
[] args)
{
int
[][] mat =
new
int
[][]
{
new
int
[] {12, 1, 14, 3, 16},
new
int
[] {14, 2, 1, 3, 35},
new
int
[] {14, 1, 14, 3, 11},
new
int
[] {14, 25, 3, 2, 1},
new
int
[] {1, 18, 3, 21, 14}
};
int
n = 5;
findAndPrintCommonElements(mat, n);
}
}