private double find(int A[], int B[], int l, int r) {
    int m = A.length, n = B.length;
    if (l > r) {
      return find(B, A, Math.max(0, (n - m) / 2), Math.min(B.length - 1, (m + n) / 2));
    }
    int i = (l + r) / 2;
    int j = (m + n) / 2 - i - 1;
    int Bj = j < 0 ? Integer.MIN_VALUE : B[j];
    int Bj1 = j >= n - 1 ? Integer.MAX_VALUE : B[j + 1];
    if (A[i] < Bj) {
      return find(A, B, i + 1, r);
    }
    if (A[i] > Bj1) {
      return find(A, B, l, i - 1);
    }
    if ((m + n) % 2 == 1) {
      return A[i];
    }
    int second = (i > 0 && j >= 0) ? Math.max(A[i - 1], Bj) : i > 0 ? A[i - 1] : Bj;
    return (A[i] + second) / 2.0;
  }