Watch 10 video solutions for Median of Two Sorted Arrays, a hard level problem involving Array, Binary Search, Divide and Conquer. This walkthrough by Tushar Roy - Coding Made Simple has 559,976 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
Given two sorted arrays nums1 and nums2 of size m and n respectively, return the median of the two sorted arrays.
The overall run time complexity should be O(log (m+n)).
Example 1:
Input: nums1 = [1,3], nums2 = [2] Output: 2.00000 Explanation: merged array = [1,2,3] and median is 2.
Example 2:
Input: nums1 = [1,2], nums2 = [3,4] Output: 2.50000 Explanation: merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5.
Constraints:
nums1.length == mnums2.length == n0 <= m <= 10000 <= n <= 10001 <= m + n <= 2000-106 <= nums1[i], nums2[i] <= 106Problem Overview: You receive two individually sorted arrays and must return the median of the combined dataset. The challenge is computing the median without actually merging both arrays, ideally faster than linear time.
Approach 1: Merge and Find Median (O(m + n) time, O(1) space)
The straightforward approach simulates the merge step of merge sort. Use two pointers, one for each array, and iterate through elements in sorted order. Track the element positions until you reach the median index (or the two middle indices when the total length is even). Since the arrays are already sorted, each step only compares the current values and advances one pointer.
This approach runs in O(m + n) time because you may scan both arrays entirely. Space can remain O(1) if you avoid building a merged array and instead only track the current and previous values. It’s easy to implement and useful for understanding the problem mechanics, but it does not meet the strict O(log(m+n)) expectation many interviewers want for this problem.
Approach 2: Binary Search on Smaller Array (O(log(min(m,n))) time, O(1) space)
The optimal solution uses binary search to partition the arrays so that the left half contains exactly half the elements of the combined arrays. Perform binary search on the smaller array and choose a partition index. The corresponding partition in the second array is derived from the total elements required on the left side.
The key condition: every element on the left side must be less than or equal to every element on the right side. Check this by comparing boundary values around both partitions. If the condition fails, adjust the partition using binary search. Once valid partitions are found, compute the median from the boundary elements depending on whether the total length is odd or even.
This technique leverages the sorted property of the arrays and turns the problem into a partition search problem. The runtime becomes O(log(min(m,n))) with constant space, making it one of the classic advanced applications of arrays, binary search, and divide and conquer.
Recommended for interviews: Interviewers usually expect the binary search partition method because it achieves logarithmic complexity and demonstrates strong algorithmic reasoning. The merge approach shows correct understanding of medians and sorted traversal, but the binary search solution shows deeper skill with partition logic and edge-case handling.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Merge and Find Median | O(m + n) | O(1) | Simple implementation when optimal logarithmic performance is not required |
| Binary Search on Smaller Array | O(log(min(m,n))) | O(1) | Best for interviews and large datasets where logarithmic time matters |