LeetCode #4: Median of Two Sorted Arrays
Find the median of two sorted arrays in O(log(min(m,n))) time using binary search on partitions.
π― What is a Median?
The median is the middle value in a sorted list:
- If the list has odd length: median = middle element
- If even length: median = average of two middle elements
Odd: [1, 2, 3, 4, 5] β median = 3
Even: [1, 2, 3, 4] β median = (2 + 3)/2 = 2.5
The Problem
Given two sorted arrays nums1
and nums2
, return the median of the two sorted arrays.
You must write an algorithm that runs in O(log(min(m,n))) time.
Examples
nums1: [1, 3] Β nums2: [2] Β β Median: 2 Β (merged array = [1,2,3] β median = 2)
nums1: [1, 2] Β nums2: [3, 4] Β β Median: 2.5 Β (merged array = [1,2,3,4] β median = (2 + 3)/2 = 2.5)
nums1: [0, 0] Β nums2: [0, 0] Β β Median: 0 Β (All zeros β median = 0)
β Brute Force: Merge & Find Median
The simplest way: merge both arrays, sort them, then find the median.
function findMedianSortedArrays(nums1, nums2) {
const merged = [...nums1, ...nums2].sort((a, b) => a - b);
const mid = Math.floor(merged.length / 2);
if (merged.length % 2 === 0) {
return (merged[mid - 1] + merged[mid]) / 2;
} else {
return merged[mid];
}
}
β Simple, but β O((m+n) log(m+n)) time β too slow!
β Optimized: Binary Search on Partition
We can't merge the arrays, but we can use binary search to find the correct split.
π Key Insight
The median divides the combined array into two halves:
- Left half: all values β€ median
- Right half: all values β₯ median
We want to split nums1
and nums2
such that:
max(left) β€ min(right)
How It Works
- We binary search on the smaller array to find the correct partition
- For each partition in
nums1
, we compute the matching one innums2
- We check if
maxLeft β€ minRight
- If yes β we found the median
- If no β adjust the binary search bounds
function findMedianSortedArrays(nums1, nums2) {
// Ensure nums1 is the smaller array
if (nums1.length > nums2.length) {
[nums1, nums2] = [nums2, nums1];
}
const m = nums1.length;
const n = nums2.length;
let left = 0;
let right = m;
while (left <= right) {
const partition1 = Math.floor((left + right) / 2);
const partition2 = Math.floor((m + n + 1) / 2) - partition1;
const maxLeft1 = partition1 === 0 ? -Infinity : nums1[partition1 - 1];
const minRight1 = partition1 === m ? Infinity : nums1[partition1];
const maxLeft2 = partition2 === 0 ? -Infinity : nums2[partition2 - 1];
const minRight2 = partition2 === n ? Infinity : nums2[partition2];
if (maxLeft1 <= minRight2 && maxLeft2 <= minRight1) {
// Correct partition found
if ((m + n) % 2 === 0) {
return (Math.max(maxLeft1, maxLeft2) + Math.min(minRight1, minRight2)) / 2;
} else {
return Math.max(maxLeft1, maxLeft2);
}
} else if (maxLeft1 > minRight2) {
right = partition1 - 1; // too far right in nums1
} else {
left = partition1 + 1; // too far left in nums1
}
}
throw new Error("Arrays are not sorted");
}
π‘ Why O(log(min(m,n)))? We binary search on the smaller array β so the number of steps is logarithmic in the smaller size.
π Real-World Applications
This "find median without merging" idea is used in systems where data is large or distributed.
π Data Analytics
When analyzing logs or user behavior across servers, you might have sorted data on each machine. Finding the global median without transferring all data saves bandwidth and time.
πΎ Database Systems
Distributed databases often keep sorted indexes. To compute statistics (like median), they use partitioning techniques instead of full merges.
π‘ Network Monitoring
Routers collect timestamped packets in sorted order. To find median latency across links, they use divide-and-conquer methods like this one.
π Financial Systems
Stock exchanges process sorted price feeds. Calculating median prices across markets efficiently is critical β and uses similar partitioning logic.
π Why This Matters: This problem teaches that binary searchisnβt just for finding values, it can be used to find optimal partitions in complex data.
Summary
This problem teaches a powerful idea: binary search doesn't just work on arrays β it can work on "partitions". By ensuring correct left/right splits, we find the median without merging.