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