C++ solution only few lines


#1
double Solution::findMedianSortedArrays(const vector<int> &A, const vector<int> &B) {
    int n1 = A.size(), n2 = B.size(), l = 0, r = 2*n2;
    if(n1 < n2) return findMedianSortedArrays(B, A);

    while(l <= r) {
        int m2 = (l + r) / 2, m1 = n1 + n2 - m2;
        double l1 = m1 == 0 ? INT_MIN : A[(m1 - 1) / 2];
        double l2 = m2 == 0 ? INT_MIN : B[(m2 - 1) / 2];
        double r1 = m1 == 2 * n1 ? INT_MAX : A[m1/2]; 
        double r2 = m2 == 2 * n2 ? INT_MAX : B[m2/2];
        if(l1 > r2) l = m2 + 1;
        else if(l2 > r1) r = m2 - 1;
        else return (max(l1, l2) + min(r1, r2)) / 2;
    }

    return -1;
}