How to Find the Median in a Row-wise Sorted Matrix Using Binary Search
The goal is to find the median element in a matrix where each row is sorted in increasing order, but the columns are not necessarily sorted.
Since the matrix is not fully sorted, we can't directly flatten and sort it efficiently. Instead, we apply a clever approach using binary search on the value range.
Important: This algorithm only relies on row-wise sorting. Each row is individually sorted in increasing order. Column sorting is not assumed and not needed.
Example Matrix:
[
[1, 3, 5],
[2, 6, 9],
[3, 6, 9]
]
Though rows are sorted, columns are not necessarily sorted (e.g., column 1 is [3,6,6]). That's fine — the logic only looks at rows.
Steps to solve:
- Step 1: Identify the minimum and maximum values in the matrix:
- Minimum = smallest first element across rows (because each row is sorted)
- Maximum = largest last element across rows
- Step 2: Do a binary search between min and max values, with mid value as the average of min and max values.
- Step 3: For each mid value:
- Use binary search in each row to count how many elements are ≤ mid (e.g., with
upper_bound
)
- Sum the counts across rows
- Step 4: If total count ≤ (r * c)/2, median must be larger →
low = mid + 1
- Step 5: Else, median is smaller or equal →
high = mid - 1
- Step 6: Repeat until low > high. The value at
low
is the median.
Example Walkthrough:
Matrix:
[
[1, 3, 5],
[2, 6, 9],
[3, 6, 9]
]
Total elements = 9 (odd), so median is the 5th smallest element.
min = 1, max = 9
Binary Search:
mid = 5 → count = 5 → go left
mid = 2 → count = 2 → go right
mid = 3 → count = 4 → go right
mid = 4 → count = 4 → go right
mid = 5 → count = 5 → go left
Ends with low = 5 → Median = 5
This works perfectly even though the matrix is not column-sorted.
Case 1 - Even number of elements
When the total number of elements is even, you may choose floor/ceil as median. Example matrix:
[[1, 3], [2, 4]]
Case 2 - All elements same
[[7, 7], [7, 7]] → Median = 7
Case 3 - One row
[[1, 2, 3, 4, 5]] → Median = 3
Case 4 - One column
[[2], [4], [6]] → Median = 4
Case 5 - Duplicates
[
[1, 2, 2],
[2, 2, 3],
[3, 3, 4]
] → Median = 2
Case 6 - Large Values
[[100000, 100001], [100002, 100003]] → Median = 100001