Median of a Row Wise Sorted Matrix - Problem

Given an m x n matrix where each row is sorted in non-decreasing order and contains an odd total number of integers, your task is to find the median of all elements in the matrix.

The median is the middle value when all elements are arranged in sorted order. Since the total count is odd, there will always be exactly one median value.

Important: You must solve this problem in less than O(m ร— n) time complexity, which means you cannot simply extract all elements and sort them.

Example: In a 3ร—3 matrix with 9 elements, the median would be the 5th smallest element when all 9 elements are conceptually sorted.

Input & Output

example_1.py โ€” Basic 3x3 Matrix
$ Input: matrix = [[1,5,8],[2,6,9],[3,6,9]]
โ€บ Output: 6
๐Ÿ’ก Note: When sorted, all elements are [1,2,3,5,6,6,8,9,9]. The median (middle element) at index 4 is 6.
example_2.py โ€” Single Row Matrix
$ Input: matrix = [[1,2,3,4,5]]
โ€บ Output: 3
๐Ÿ’ก Note: In a single row with 5 elements, the median is the middle element at index 2, which is 3.
example_3.py โ€” Single Column Matrix
$ Input: matrix = [[1],[2],[3]]
โ€บ Output: 2
๐Ÿ’ก Note: In a single column with 3 elements, the median is the middle element, which is 2.

Visualization

Tap to expand
๐Ÿ“š Library Shelf Search StrategyShelf 1150p280p340p420pShelf 2180p250p310p380p๐ŸŽฏ Strategy: Binary Search1. Guess median = 300 pages2. Count books โ‰ค 300p per shelf: Shelf 1: 3 books, Shelf 2: 3 books3. Total: 6 โ‰ฅ 5 needed โ†’ try smallerโœ… ResultAfter iterations: Median = 280 pagesFound without moving any books!Time: O(shelves ร— log(books_per_shelf) ร— log(range))๐Ÿ’ก Key Insight: Use organization to count without collecting!
Understanding the Visualization
1
Identify Range
Note the smallest and largest page counts across all shelves
2
Make a Guess
Pick a middle value for potential median page count
3
Count Efficiently
For each shelf, quickly count books with โ‰ค guessed pages using shelf organization
4
Adjust Guess
If too few books counted, guess higher; if too many, guess lower
5
Converge
Repeat until finding the exact median page count
Key Takeaway
๐ŸŽฏ Key Insight: Since rows are pre-sorted, we can efficiently count elements smaller than any value using binary search, enabling us to binary search on the answer itself rather than sorting all elements.

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(m * log(n) * log(max-min))

Binary search on answer space: log(max-min) iterations, each taking O(m * log(n)) to count elements

n
2n
โšก Linearithmic
Space Complexity
O(1)

Only using a few variables for binary search, no extra arrays needed

n
2n
โœ“ Linear Space

Constraints

  • m == grid.length
  • n == grid[i].length
  • 1 โ‰ค m, n โ‰ค 500
  • m * n is odd
  • 1 โ‰ค grid[i][j] โ‰ค 106
  • Each row is sorted in non-decreasing order
Asked in
Google 42 Amazon 38 Meta 29 Microsoft 24 Apple 18
52.2K Views
Medium-High Frequency
~25 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen