Median in a row-wise sorted Matrix
Problem Statement:β
Given aΒ row-wise sortedΒ matrixΒ mat[][]Β of size n*m, where the number of rows and columns is alwaysΒ odd.Β Return theΒ medianΒ of the matrix.
-
Example:
Examples:
Input: mat[][] = [[1, 3, 5],
[2, 6, 9],
[3, 6, 9]]
Output:Β 5
Explanation: Sorting matrix elements gives us [1, 2, 3, 3, 5, 6, 6, 9, 9]. Hence, 5 is median.Input:mat[][] = [[2, 4, 9],
[3, 6, 7],
[4, 7, 10]]
Output:6
Explanation: Sorting matrix elements gives us [2, 3, 4, 4, 6, 7, 7, 9, 10]. Hence, 6 is median.Input:mat = [[3], [4], [8]]
Output:4
Explanation: Sorting matrix elements gives us [3, 4, 8]. Hence, 4 is median.
Solution: Binary Search on Answerβ
class Solution {
public:
int median(vector<vector<int>> &mat) {
int n = mat.size();
int m = mat[0].size();
// Find min element (first col elements) and max element (last col elements)
int minVal = INT_MAX, maxVal = INT_MIN;
for(int i = 0; i < n; i++){
minVal = min(mat[i][0], minVal);
maxVal = max(mat[i][m-1], maxVal);
}
// Median will be the (n*m + 1)/2-th element (1-based indexing)
int desired = (n * m + 1) / 2;
int low = minVal, high = maxVal;
while(low < high){
int mid = low + (high - low) / 2;
// Count elements <= mid using upper_bound in each row
int placed = 0;
for(int i = 0; i < n; i++){
placed += upper_bound(mat[i].begin(), mat[i].end(), mid) - mat[i].begin();
}
// If count is less than desired, go right (increase low)
if(placed < desired)
low = mid + 1;
else
high = mid;
}
return low; // low = median at the end
}
};
π How It Worksβ
- Each row of the matrix is sorted.
- The median is the element at position
(n*m + 1)/2in the flattened sorted array. - Instead of merging rows, we binary search on the value range (
minValtomaxVal). - For a guessed
mid, count how many elements are<= midusingupper_bound(binary search in each row). - If count is less than desired position, move right (
low = mid+1), else move left. - Eventually
lowconverges to the median.
π§© Key Formula / Recurrenceβ
-
Desired position:
-
Transition during binary search:
- If
count(mid) < desiredβlow = mid + 1 - Else β
high = mid
- If
β±οΈ Time & Space Complexityβ
-
Counting step per mid: (
upper_boundin each row). -
Binary search range: .
-
Total:
-
Space: O(1) (no extra structures).
β οΈ Edge Casesβ
- Matrix with 1 row or 1 column.
- All elements same (minVal == maxVal β median is that element).
- Large numbers in rows (watch for overflow in
(low+high)/2).
π‘ Other Approachesβ
- Flatten & Sort: O(nm log(nm)) time, O(n*m) space β (not efficient).
- Heap approach: Keep min-heap of row pointers β O(n log n + k log n) but more complex.
- Binary Search on Value (this solution) β Best for row-wise sorted matrix.
π Related Problemsβ
- Median of Two Sorted Arrays (LeetCode 4)
- Kth Smallest Element in a Sorted Matrix (LeetCode 378)
- Search a 2D Matrix II (LeetCode 240)
π Level Up Your Engineering Mindset
Never Study in Isolation. Join Binary Dose.
Connect with fellow engineers, clear conceptual doubts, and get 2 visual breakdowns & engineering mental models daily across our community channels.
Official Hub
Discord Community
Start at #welcome-and-rules, say hello in #general-chat, clear doubts, and collaborate with engineers worldwide.