Matrix Median
Rows of the matrix are sorted. Find the overall median without flattening (odd element count).
Open on GeeksforGeeks ↗Intuition
The median is the smallest value with more than half the elements ≤ it. 'How many elements ≤ x' is countable fast — binary search each sorted row — and it's monotone in x, so binary search the value range for the first x whose count exceeds n·m/2.
Approach
Binary search on value, not position
Search lo=min(first column) to hi=max(last column). No element order across rows needed.
Count ≤ mid per row
Each sorted row answers 'how many ≤ mid' via bisect in O(log m); total count in O(n log m).
First value passing half wins
If count ≤ half, median is bigger → lo = mid+1; else hi = mid. Loop converges on the median itself (it must be present because counts jump only at matrix values).
Solution & live demo
Edge cases
Median is the middle of that row — the search still lands there.
Counting ≤ handles ties naturally; convergence lands on the duplicated value.