double findMedianSortedArraysInternal(int[] A, int[] B) { int m = A.length; int n = B.length;
int k = (n + m - 1) / 2; int l = 0, r = m; // this is m not m-1 while (l < r) { int mid = l + (r - l) / 2, bIdx = k - mid; if (bIdx > k || A[mid] < B[bIdx]) { l = mid + 1; } else { r = mid; } }
int a = l - 1 >= 0? A[l - 1] : Integer.MIN_VALUE; int b = k - l >= 0? B[k - l] : Integer.MIN_VALUE; a = a >= b ? a : b; if( (n + m) % 2 == 1) return a; int c = k - l + 1 >= n? Integer.MAX_VALUE: B[k - l + 1]; int d = l >= m? Integer.MAX_VALUE: A[l]; c = c <= d ? c : d; return ((double) (a + c)) / 2.0; }
double findMedianSortedArraysInternal(int[] A, int[] B) { int m = A.length; int n = B.length;
int k = (n + m - 1) / 2; int l = 0, r = m; // this is m not m-1 while (l < r) { int mid = l + (r - l) / 2, bIdx = k - mid; if (bIdx > k || A[mid] < B[bIdx]) { l = mid + 1; } else { r = mid; } }
int a = l - 1 >= 0? A[l - 1] : Integer.MIN_VALUE; int b = k - l >= 0? B[k - l] : Integer.MIN_VALUE; a = a >= b ? a : b; if( (n + m) % 2 == 1) return a; int c = k - l + 1 >= n? Integer.MAX_VALUE: B[k - l + 1]; int d = l >= m? Integer.MAX_VALUE: A[l]; c = c <= d ? c : d; return ((double) (a + c)) / 2.0; }