This solution O(lg(m + n)) is not good for interview. Try kth smallest element solution extension.
public class MedianTwoSortedArrays {
private double findMedian(int A[], int B[], int left, int right) {
int m = A.length, n = B.length, mid = (m + n) / 2;
if (left > right) {
return findMedian(B, A, Math.max(0, mid - m), Math.min(n - 1, mid));
}
int i = (left + right) / 2, j = mid - i - 1;
if (j >= 0 && A[i] < B[j]) // A[i] < median < B[j]
return findMedian(A, B, i + 1, right);
if (j < n - 1 && A[i] > B[j + 1]) // A[i] > median > B[j + 1]
return findMedian(A, B, left, i - 1);
// found median
// m+n is odd
if (((m + n) & 0x1) > 0 || (i <= 0 && (j < 0 || j >= n)))
return A[i];
// m+n is even
if (j < 0 || j >= n)
return (A[i] + A[i - 1]) / 2.0;
if (i <= 0)
return (A[i] + B[j]) / 2.0;
return (A[i] + Math.max(B[j], A[i - 1])) / 2.0;
}
public double findMedianSortedArrays(int A[], int B[]) {
// Start typing your Java solution below
// DO NOT write main() function
int m = A.length, n = B.length;
if (m < n)// median in A
return findMedian(A, B, 0, m - 1);
else// median in B
return findMedian(B, A, 0, n - 1);
}
}
O(n), through kth smallest function.
public class Solution {
public double findMedianSortedArrays(int A[], int B[]){
// Start typing your Java solution below
// DO NOT write main() function
int m = A.length, n = B.length;
if ((m + n) % 2 == 0)// median in A
return (findKthSmallest(A, m , B, n, (m + n) / 2 + 1) + findKthSmallest(A, m , B, n, (m + n) / 2)) / 2.0;
else// median in B
return findKthSmallest(A, m , B, n, (m + n) / 2 + 1);
}
public double findKthSmallest(int A[], int m, int B[], int n, int k){
int ia = 0, ib = 0, res = 0;
while(--k >= 0 && ia <m && ib < n){
if(A[ia] < B[ib])
res = A[ia++];
else
res = B[ib++];
}
while(ia < m && k-- >=0)
res = A[ia++];
while(ib < n && k-- >=0)
res = B[ib++];
return res;
}
}