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.
This approach leverages binary search to reduce the problem to a smaller size, achieving the desired O(log(m + n)) complexity. The strategy involves performing binary search on the smaller array to find the perfect partition point.
In this C solution, we first ensure that we perform the binary search on the smaller of the two arrays to maintain efficiency. We set bounds for `i` and adjust those bounds based on comparisons calculated at each step. The correct partitioning of elements between the two arrays determines the median.
C++
Java
Python
C#
JavaScript
Time complexity: O(log(min(m,n))). Space complexity: O(1).
This approach focuses on merging the two sorted arrays as naturally done in merge sort, and then finding the median directly from the merged result. Though this solution has a higher time complexity, it's easier to implement and understand.
The C implementation uses an auxiliary array to merge the two sorted arrays into one. After merging, we calculate the median of the merged array based on its even or odd length. Memory allocation and deallocation are crucial to manage space efficiently.
C++
Java
Python
C#
JavaScript
Time complexity: O(m + n). Space complexity: O(m + n).
| Approach | Complexity |
|---|---|
| Binary Search on Smaller Array | Time complexity: O(log(min(m,n))). Space complexity: O(1). |
| Merge and Find Median | Time complexity: O(m + n). Space complexity: O(m + n). |
| 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 |
Binary Search : Median of two sorted arrays of different sizes. • Tushar Roy - Coding Made Simple • 559,976 views views
Watch 9 more video solutions →Practice Median of Two Sorted Arrays with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor