public double dfs(int A[], int B[], int indexA, int indexB, int k) {
        int lenA = A.length, lenB = B.length;
        if (indexA >= lenA)
            return B[indexB + k - 1];
        else if (indexB >= lenB)
            return A[indexA + k - 1];
        if (k == 1)
            return Math.min(A[indexA], B[indexB]);

        int mid = k / 2 - 1;
        int keyA = indexA + mid >= lenA ? Integer.MAX_VALUE : A[indexA + mid];
        int keyB = indexB + mid >= lenB ? Integer.MAX_VALUE : B[indexB + mid];
        int kNew = k - k / 2;
        if (keyA < keyB)
            return dfs(A, B, indexA + k / 2, indexB, kNew);
        else
            return dfs(A, B, indexA, indexB + k / 2, kNew);
    }