202. Matrix Median

Given a 2D array matrix that is row-wise sorted. The task is to find the median of the given matrix.

Example 1:

Input: matrix=[ [1, 4, 9], [2, 5, 6], [3, 7, 8] ] 

Output: 5

Explanation: If we find the linear sorted array, the array becomes 1 2 3 4 5 6 7 8 9. So, median = 5

Example 2:

Input: matrix=[ [1, 3, 8], [2, 3, 4], [1, 2, 5] ] 

Output: 3

Explanation: If we find the linear sorted array, the array becomes 1 1 2 2 3 3 4 5 8. So, median = 3

Now Your Turn!

Pick the correct output for the given input

Input: matrix=[ [1, 4, 15], [2, 5, 6], [3, 8, 11] ] 

Still unsure what the problem is asking ?

Let’s go through a few more examples, step by step, to make it clearer.

Constraints:

  •   N==matrix.size
  •   M==matrix[0].size
  •   1 <= N, M <= 105
  •   1 <= N*M <= 106
  •   1 <= matrix[i] <= 109
  •  N*M is odd

Hints

Frequently Occurring Doubts

Interview Follow-up Questions

Fun Facts

0
class Solution{
public:
int findMedian(vector<vector<int>>&matrix) {
}
};
Test Case

Input:

Matrix