


Assume that the size of the matrix N*M is always odd.
The first line of input contains a single integer T, representing the number of test cases or queries to be run.
Then the T test cases follow.
The first line of each test case contains two space separated integers N & M, the number of rows and columns respectively.
The next N lines of each test case contains M single space separated integers each, representing the elements of the matrix.
For each test case, print an integer denoting the median of the matrix.
The output of each test case is printed in a separate line.
You do not need to print anything, it has already taken care of. Just implement the given function.
1 <= T <= 10
1 <= N,M <= 10^4
-10^8 <= matrix[i][j] <= 10^8
Where 'matrix[i][j]' is the value of the element at 'jth' column of 'ith' row of the matrix.
Time Limit: 1 sec
The simplest way to find the median of the input matrix is to convert it into a 1D sorted array. Take an array of size N*M and push all the elements of the input matrix in the array. Sort the array and return the middle value, that is the median.
Another simplest way to use a data structure which keeps its elements sorted, that is an ordered map. Insert all the elements of the given matrix in the map such that the key is the element and the corresponding value is the number of times the element occurs in the matrix. Iterate the map and return the middle element, that is the median.
The rows of the input matrix are sorted, so we can use this property of the matrix to solve the problem. Binary search works on sorted arrays and thus binary search can be used to find median in this case. The idea is that for a number to be median there should be exactly (N/2) elements that are less than it. So, find that number using binary search.