4. Median of Two Sorted Arrays¶
Edited by Charles
Hard · Python · Array
Problem
Read the original problem on LeetCode.
The official page contains the problem statement, examples, and constraints.
Explanation, examples & other approaches
Core idea
Binary-search a partition of the shorter array. Find a split where every left-side value is no larger than every right-side value, then read the middle value(s).
Original study notes follow, including intermediate snippets and earlier attempts.
Idea¶
Use Binary Search + Partition.
Instead of actually merging the two sorted arrays, divide both arrays into a left part and a right part such that:
- The left part contains half of all elements.
- Every element in the left part is smaller than or equal to every element in the right part.
Then the median can be determined directly from the elements around the two partitions.
To achieve O(log(min(m, n))), always perform binary search on the shorter array.
Always Binary Search the Shorter Array¶
Let:
Make sure:
Therefore:
We only binary search the partition position in nums1.
The possible partition positions are:
Therefore:
Partition the Two Arrays¶
Suppose i is the partition position in nums1:
i also represents the number of elements from nums1 placed in the left half.
Choose:
The total number of elements required in the left half is:
Therefore, if nums1 contributes i elements, nums2 must contribute:
So:
The +1 allows the left half to contain one extra element when the total number of elements is odd.
Four Boundary Values¶
After partitioning:
Define:
However, the partition may occur at the beginning or end of an array.
For example:
There is no left element, so treat it as:
Similarly:
has no right element, so treat it as:
Therefore:
leftA = nums1[i - 1] if i > 0 else -float('inf')
rightA = nums1[i] if i < m else float('inf')
leftB = nums2[j - 1] if j > 0 else -float('inf')
rightB = nums2[j] if j < n else float('inf')
Correct Partition Condition¶
Because both arrays are already sorted, we already know:
Therefore, we only need to check the cross-array relationships:
If both conditions hold:
then every element in the combined left half is less than or equal to every element in the combined right half.
Binary Search Direction¶
Case 1: leftA > rightB¶
nums1 contributes too many elements to the left half.
Therefore, i is too large and should move left:
Case 2: leftB > rightA¶
This means nums1 contributes too few elements to the left half.
Therefore, i is too small and should move right:
A useful way to remember:
Calculate the Median¶
Once the correct partition is found:
Odd Total Length¶
Because the left half contains one extra element:
Therefore:
Even Total Length¶
The two middle elements are:
largest element in the left half
= max(leftA, leftB)
smallest element in the right half
= min(rightA, rightB)
Therefore:
Example¶
Since nums1 must be the shorter array, swap them:
The left half needs:
elements.
First partition:
So:
We have:
Since:
nums1 contributes too few elements to the left side.
Move i right.
Next:
Partition:
Now:
Both conditions hold:
The partition is correct.
Since the total length is odd:
Complexity¶
Let m <= n.
Time Complexity:
because binary search is performed only on the shorter array.
Space Complexity:
No additional arrays are created.
Key Points¶
i = number of nums1 elements in the left half
j = number of nums2 elements in the left half
i + j = (m + n + 1) // 2
Correct partition:
Binary search direction:
Median:
Study record
| Field | Value |
|---|---|
| Attempts | 1 |
| Mastery | ❚ |
| The Latest Attempt | September 7, 2026 |
Python solution¶
class Solution:
def findMedianSortedArrays(self, nums1: List[int], nums2: List[int]) -> float:
if len(nums1) > len(nums2):
nums1, nums2 = nums2, nums1
m, n = len(nums1), len(nums2)
left, right = 0, m
while left <= right:
i = (left + right) // 2
j = (m + n + 1) // 2 - i
leftA = nums1[i - 1] if i > 0 else -float('inf')
rightA = nums1[i] if i < m else float('inf')
leftB = nums2[j - 1] if j > 0 else -float('inf')
rightB = nums2[j] if j < n else float('inf')
if leftA <= rightB and leftB <= rightA:
if (m + n) % 2:
return max(leftA, leftB)
else:
return (
max(leftA, leftB)
+ min(rightA, rightB)
) / 2
elif leftA > rightB:
right = i - 1
else:
left = i + 1