#C9040. Finding the Smallest Element Larger than k

    ID: 53090 Type: Default 1000ms 256MiB

Finding the Smallest Element Larger than k

Finding the Smallest Element Larger than k

You are given an n × n matrix of integers with 2 \le n \le 100 and an integer k. Your task is to find the smallest element in the matrix that is strictly larger than k. If no such element exists, output -1.

Problem Details:

  • The matrix is provided as n rows with n space-separated integers each.
  • You need to process multiple test cases.
  • If there is more than one element larger than k, output the minimum among them. If no element is larger than k, output -1.

In mathematical notation, if the matrix is denoted as \(A\) and \(k\) is the reference integer, you need to compute:

[ \min{a_{ij} \mid a_{ij} > k} ]

with the convention that if the set is empty, the answer is \(-1\).

inputFormat

The first line of the input contains an integer T indicating the number of test cases.

For each test case, the first line contains two integers n and k, where n is the dimension of the matrix and k is the reference number.

This is followed by n lines, each containing n space-separated integers representing the rows of the matrix.

Input Format Example:

T
n k
a11 a12 ... a1n
a21 a22 ... a2n
...
an1 an2 ... ann

outputFormat

For each test case, output a single line containing the smallest element in the matrix that is larger than k. If no such element exists, output -1.

Output Format Example:

result1
result2
... 
resultT
## sample
1
3 5
1 6 3
7 4 9
2 8 5
6

</p>