Matrix Median
Sorted Rows
Data Structures
Algorithm
Computational Mathematics

Median of a Matrix with sorted rows

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Understanding the Median of a Matrix with Sorted Rows

The concept of finding the median in a matrix, particularly when the rows are sorted, is a fascinating problem that combines elements of matrix manipulation with statistical analysis. This article aims to explore this concept in depth, providing technical insights, examples, and a comprehensive summary table to distill the information.

What is the Median?

Before delving into matrices, let's reiterate what a median is. In statistics, the median is the value that separates a dataset into two equal halves, with half of the numbers being larger and half being smaller than this value. For an odd number of elements, it is the middle number. For an even number, it is typically the average of the two middle numbers.

The Problem: Median in a Matrix with Sorted Rows

Finding the median of a matrix with sorted rows involves identifying the middle element when all elements of the matrix are considered in a single sorted list. This problem can be challenging due to the matrix's dimensions and the need to maintain computational efficiency.

Matrix Configuration

Consider a matrix `M` of dimensions `n x m`, where each row is independently sorted. For example:

  • Row 1: `[1, 3, 5]`
  • Row 2: `[2, 6, 9]`
  • Row 3: `[3, 6, 9]`
    • Approach: Concatenate all rows into a single array and sort it, then find the median.
    • Complexity: This method has a time complexity of O(n×mlog(n×m))O(n \times m \log(n \times m)) due to sorting.
    • Drawback: This method is not efficient for large matrices because of the sorting overhead.
    • Approach: Use the properties of rows being sorted to apply a binary search for the median value.
    • Procedure:
    • Complexity: This improves the time complexity to O(n×log(m)×log(maxmin))O(n \times \log(m) \times \log(max - min)).
    • Advantages: More efficient, especially with large matrices.
  • Step 1: Determine `min_val = 1` and `max_val = 9`.
  • Step 2: Set `low = 1` and `high = 9`.
  • Step 3: Conduct binary search:
    • Calculate `mid_val = (low + high) / 2`.
    • Check how many numbers are `<= mid_val` using an auxiliary function.
    • Adjust `low` or `high` based on the count.
  • Handling Non-square Matrices: The procedure is the same for non-square matrices; only index calculations change, but binary search remains applicable.
  • Edge Cases: Ensure that matrices with even total numbers handle the median averaging correctly if required.

Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.