#K46212. Smallest Common Element in Matrix Rows
Smallest Common Element in Matrix Rows
Smallest Common Element in Matrix Rows
You are given a matrix of integers. Each row of the matrix is sorted in non-decreasing order. Your task is to find the smallest common element that appears in every row of the matrix.
If there is no such common element, output \( -1 \).
Note: The rows may contain duplicate values but you should consider each distinct element in a row only once.
inputFormat
The input is given via standard input (stdin). The first line contains a single integer \( n \) which represents the number of rows in the matrix. Each of the following \( n \) lines represents a row of the matrix and contains space-separated integers.
You may assume that \( n \ge 0 \). In case \( n = 0 \), the matrix is empty and the answer is \( -1 \).
outputFormat
Print a single integer which is the smallest common element in all rows of the matrix. If no common element exists, print \( -1 \).
## sample4
1 2 3 4 5
2 4 5 8 10
3 5 7 9 11
1 3 5 7 12
5