Solution in C++;


#1
double Solution::findMedianSortedArrays(const vector<int> &A, const vector<int> &B) {
    int n = A.size();
    int m =  B.size();
    if(n!=0 && m!=0){
        if(n<m) return findMedianSortedArrays(B,A);
        else
        {
            int low=0;
            int high=m-1;
            while(low<=high)
            {
                int mid=(low+high)/2;
                
                int x1,y1,x2,y2;
                y1 = B[mid];
                y2 = (mid==m-1)?INT_MAX:B[mid+1];
                x2 = A[((m+n+1)/2)-(mid+1)];
                x1 = ((((m+n+1)/2)-(mid+1))==0)?INT_MIN:A[((m+n+1)/2)-(mid+1)-1];
                
                if(x1<=y2 && y1<=x2)
                {
                    double ans = ((m+n)%2!=0)?max(x1,y1):(double)(max(x1,y1)+min(x2,y2))/2; 
                    return ans;
                }
                else if(y1>x2)  high = mid-1;
                else if(x1>y2)  low = mid+1;
            }
            int x1 = A[((n+m+1)/2)-1];
            int x2 = min(B[0],A[((n+m+1)/2)]);
            double ans = ((m+n)%2!=0)?x1:(double)(x1+x2)/2;
            return ans;
        }
    }
    else if(n==0)
    {
        if(m%2==0)  return (double)(B[m/2]+B[(m/2)-1])/2;
        else    return (double)B[m/2]; 
    }
    else if(m==0)
    {
        if(n%2==0)  return (double)(A[n/2]+A[(n/2)-1])/2;
        else    return (double)A[n/2]; 
    }
}