Median of 5 sorted arrays
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
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.

