Median of 5 sorted arrays
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Problem Overview
Finding the median of a single sorted array is straightforward—a task efficiently solved by selecting the middle element(s), depending on whether the array length is odd or even. However, determining the median from five sorted arrays can be more complex. The challenge involves ensuring that this is done with efficiency and precision, leveraging the pre-sorted nature of the inputs.
Definitions and Initial Thoughts
Median: The median of a dataset is the middle value when the numbers are arranged in ascending order. For an even number of elements, this often represents the average of the two middle values.
Given five sorted arrays, the goal is to find the median of their combined data efficiently. The most direct approach is to merge all five arrays into one large array and then find the median. However, this merging can be computationally expensive, demanding further optimization.
Approaches to Finding the Median
Method 1: Full Merge and Median
This straightforward approach involves merging all arrays into a single sorted array. For sorted arrays , having lengths , complete the following steps:
- Merge Arrays: Utilize the merge step of the merge sort algorithm to combine the arrays. Given the array lengths, this step executes in time, where is the total number of elements, and .
- Compute Median: Once a fully sorted array is achieved, locate the median. • If the total count of elements is odd, the median is at index . • If is even, calculate the average of elements at indices
$\frac\{N\}\{2\}$ and $\frac\{N\}\{2\} - 1$.
Method 2: Median of Medians Approach
Implement a more sophisticated approach using the "Median of Medians" algorithm:
Steps:
- Initial Estimation: • Calculate the median for each array individually: . • Apply a modified median-of-medians strategy to estimate the overall median.
- Binary Search Across Median Estimates: • Use a binary search strategy across these medians and determine the relative quantities of numbers less than and greater than the median estimate. • Develop 'cut' points for each array and iteratively select potential median candidates.
- Iterate to Convergence: • Continuously refine the potential solution until convergence at true median value or the difference becomes negligible.
Comparison of Methods
| Method | Time Complexity | Space Complexity | Suitability |
| Full Merge and Median | Useful for small , simple to implement. | ||
| Median of Medians Approach | (theoretical) | More efficient for large datasets and constant . |
Algorithm Pseudocode
Here's a high-level pseudocode for the full merging technique:
• Handling Edge Cases: Consider scenarios like empty arrays or arrays with highly skewed sizes. • Memory Constraints: Prefer in-place methods or those with minimal auxiliary storage needs when working with very large datasets.
Related reading
- Median of a Matrix with sorted rows
- Median of medians algorithm why divide the array into blocks of size 5
- median of three values strategy
- Meeting Conflict algorithms
- Memoize multi-dimensional recursive solutions in haskell
- Memory-efficient way of computing the median of a large data set?
- Memoization or Tabulation approach for Dynamic programming
- memoize any given recursive function in JavaScript

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 courseTrack 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.