Design an algorithm to find the median of two sorted arrays with O(log(min(n,m))) time complexity. Use binary search on partition points, handle edge cases where one array is empty or arrays have uneven lengths, and provide a mathematical proof of correctness in comments.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
I'm providing a binary search–based algorithm on the smaller array to find the median in O(log min(n,m)), including handling empty arrays and edge partitions, plus a concise mathematical proof of correctness in comments.
A well‑known way to get (O(\log \min(m,n))) time is to search on a partition of the smaller array.
Let (A) and (B) be the input arrays of lengths (m) and (n) (assume (m\le n), swap if not), and let (k = \left\lfloor\frac{m+n+1}{2}\right\rfloor) be the size of the “left half” of the combined multiset. We want to choose a split point (i\in[0,m]) in (A) and (j=k-i) in (B) so that
all elements to the left of (i) in (A) and to the left of (j) in (B) are (\le) all elements to the right of (i) in (A) and to the right of (j) in (B).
Once such a partition is found, the median is (\max(A_{i-1}, B_{j-1})) if (m+n) is odd, or the average of (\max(A_{i-1},B_{j-1})) and (\min(A_i,B_j)) if (m+n) is even, with out‑of‑bounds indices treated as (-\infty) or (+\infty) appropriately.
Because (i) must lie between 0 and (m), we can find it by binary search:
python
def median_two_sorted(A, B):
# Ensure A is the smaller array
if len(A) > len(B):
A, B = B, A
m, n = len(A), len(B)
if m == 0:
# Edge case: one array empty
mid = n // 2
return B[mid] if n % 2 else (B[mid-1] + B[mid]) / 2.0
k = (m + n + 1) // 2 # size of left half
lo, hi = 0, m
while lo <= hi:
i = (lo + hi) // 2
j = k - i
A_left = A[i-1] if i > 0 else float('-inf')
A_right = A[i] if i < m else float('inf')
B_left = B[j-1] if j > 0 else float('-inf')
B_right = B[j] if j < n else float('inf')
# Move partition in A to satisfy A_left <= B_right and B_left <= A_right
if A_left > B_right:
hi = i - 1 # i too big, move left
elif B_left > A_right:
lo = i + 1 # i too small, move right
else:
# Correct partition found
if (m + n) % 2: # odd total length
return max(A_left, B_left)
else:
return (max(A_left, B_left) + min(A_right, B_right)) / 2.0
The loop runs in (O(\log m)) iterations because we halve the search interval ([lo, hi]) each time.
Mathematical proof of correctness (sketch):
Let (i) and (j) be the partition indices returned. By construction (i+j=k), so there are (k) elements on the left side in total. We maintain the invariants (0\le i\le m), (0\le j\le n).
The loop adjusts (i) until it satisfies (A_{i-1}\le B_j) and (B_{j-1}\le A_i) (interpreting out‑of‑bounds as (\pm\infty)), which is exactly the condition that every element on the left of the combined partition is (\le) every element on the right.
When those inequalities hold, the multiset (L = {A[0..i-1]}\cup{B[0..j-1]}) consists of the (k) smallest elements of (A\cup B), and (R = {A[i..]}\cup{B[j..]}) the remaining elements. This follows because any element of (L) is (\le) any element of (R), so no element of (R) can be among the (k) smallest.
If (m+n) is odd, the median is the maximum of (L), which is (\max(A_{i-1}, B_{j-1})). If (m+n) is even, the median is the average of the two middle elements, which are (\max(A_{i-1}, B_{j-1})) (the largest of (L)) and (\min(A_i,B_j)) (the smallest of (R)). This matches the returned values.
Thus the algorithm returns the median of the multiset union of (A) and (B) in (O(\log \min(m,n))) time.