VOOZH about

URL: https://www.geeksforgeeks.org/dsa/find-median-row-wise-sorted-matrix/

⇱ Median in row wise sorted matrix - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Median in row wise sorted matrix

Last Updated : 28 Mar, 2026

Given a row-wise sorted matrix mat[][] with an odd number of rows and columns, find the median of the matrix.
Note: The median is defined as the middle element in the sorted list of all elements in the matrix. Since the total number of elements is always odd, the median is guaranteed to exist and be unique.

Examples: 

Input: mat[][] = [[1 3 5],
[2 6 9],
[3 6 9]]
Output: 5
Explanation: Elements in sorted order: 1 2 3 3 5 6 6 9 9. There are total 9 elements, thus the median is the element at index 5 (1-based) i.e. 5.

Input: mat[][] = [[1 3 4],
[2 5 6]
[3 7 9]]
Output: 4
Explanation: Elements in sorted order: 1 2 3 3 4 5 6 7 9. There are total 9 elements, thus the median is the element at index 5 (1-based) i.e. 4.

[Naive Approach] Using Sorting - O(n × m × log(n × m)) Time and O(n × m) Space

The idea is to first flatten the 2D matrix into a 1D array by collecting all its elements. Then, the array is sorted to bring all elements into order. Since the total number of elements is odd, the median is the middle element of the sorted array.


Output
5

[Better Approach] Using Priority Queue

The idea is to use a min-heap–like structure to efficiently find the median without flattening or sorting the entire matrix. Since each row is already sorted, we start by inserting the first element of each row into a heap (simulated with a sorted map). Then we repeatedly extract the smallest element and push the next element from the same row, simulating the merging of sorted rows. This continues until we reach the median position. It avoids full sorting and leverages the sorted rows for efficiency.


Output
5

Time complexity: O(n × m × log(n)), since we are going to insert and remove the top element in priority queue (n × m) / 2 times and every time the priority queue will be having elements in it.
Auxiliary Space: O(n), the min-heap stores at most one element per row of the matrix at any point in time.

[Expected Approach] Using Binary Search on Answer

The key idea is that for a number x to be the median in an n x m matrix, there must be exactly (n * m) / 2 elements less than or equal to x. We perform binary search over the range [minElement, maxElement], where minElement and maxElement are the smallest and largest elements in the matrix.
At each step, we compute the number of elements less than or equal to the current mid.
=> If this count is less than or equal to (n * m) / 2, we search in the upper half of the range to increase the candidate value.
=> Otherwise, we search in the lower half to reduce it


Output
5

Time Complexity: O(n × log(m) × log(maxVal - minVal)), the upper bound function will take log(m) time and is performed for each row. And binary search is performed from minVal to maxVal. 
Auxiliary Space: O(1) 

Comment