Practice Median of Two Sorted Arrays
Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.
Problem Statement
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)). Practice approach: Binary-search a partition of the shorter array. Choose the other array’s partition so the left side contains floor((m + n + 1) / 2) entries. A partition is valid when each left-side maximum is at most the opposite right-side minimum. Treat a missing left neighbor as negative infinity and a missing right neighbor as positive infinity. If the shorter array’s left value is too large, move its partition left; if its right value is too small, move right. For an odd total, return the larger left value; for an even total, average that value and the smaller right value. For [1, 2] and [3, 4], the valid boundary separates 2 from 3, giving 2.5. An empty input array is handled by the same boundary rules. The search takes O(log(min(m, n) + 1)) steps, with constant work for an empty shorter array, and O(1) auxiliary space.
Source: Median of Two Sorted Arrays on LeetCode. Use the original task for its coding environment and submissions.
Examples
# Example 1
nums1 = [1,3], nums2 = [2]
2.00000
# Example 2
nums1 = [1,2], nums2 = [3,4]
2.50000
Constraints
How to solve Median of Two Sorted Arrays
Original notes for interview preparation, written for this problem. Reviewed .
Key insight
The median splits the combined elements into left and right halves of known sizes, so instead of merging you can binary search how many elements of the shorter array fall on the left; the longer array's share is then forced, and four boundary values decide whether the split is correct.
Why brute force is not enough
Merge in O(m + n) and read the middle, or concatenate and sort. With m + n up to 2000 both are instant, but the problem demands O(log(m + n)), so an interviewer will not accept them.
Approach
- Make the first array the shorter one by swapping if needed, so the search range is as small as possible.
- Let half be floor((m + n + 1) / 2), the left-side size. Binary search i, the count taken from the shorter array, from 0 to m; the longer array contributes j = half - i.
- For a candidate i, compare the last element of each left part with the first element of the opposite right part, treating an empty left part as negative infinity and an empty right part as positive infinity.
- If the shorter array's left value exceeds the longer array's right value, search lower; if the longer array's left value exceeds the shorter array's right value, search higher.
- Otherwise the partition is correct: for an odd total return the larger left value; for an even total average it with the smaller right value.
Walkthrough of Example 1
nums1 = [1,3], nums2 = [2] → 2.00000
- The shorter array is [2] (m = 1), the longer is [1,3] (n = 2), and half is floor(4 / 2) = 2.
- Try i = 0, j = 2: the shorter left part is empty and its right value is 2; the longer left part [1,3] ends in 3 and its right part is empty. Since 3 exceeds 2, i is too small.
- Try i = 1, j = 1: the shorter left value is 2 with an empty right part; the longer left value is 1 and its right value is 3. Both 2 <= 3 and 1 <= infinity hold, so the partition is correct.
- The total of 3 is odd, so the median is the larger left value, max(2, 1) = 2, reported as 2.00000.
Time and space
Time O(log(min(m, n))) · Space O(1). Each step halves the range of i over the shorter array with constant work; m and n are the array lengths and only a few indices are stored.
Interview pitfalls
- Binary searching over the longer array, which can push j negative or past the other array's length.
- Using floor((m + n) / 2) as the left size instead of floor((m + n + 1) / 2), which breaks the odd case because the median must land on the left.
- Reading before i or j when a part is empty (i = 0, i = m, j = 0, or j = n); the infinity sentinels exist to avoid that.
- Returning an integer for the even case; the result must be a floating-point average such as 2.5.
Follow-up questions interviewers ask
- How would you find the k-th smallest element across two sorted arrays, generalizing this partition?
- How does the approach change for k sorted arrays, and what complexity is achievable?
- If the arrays only support sequential reads from disk, how would you locate the median?
Further reading on the technique: Binary search algorithm
Related LeetCode Problems
Practice more problems similar to Median of Two Sorted Arrays.
Review your practice with PhantomCodeAI
After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.