public double findKth(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];
        if (indexB >= lenB)
            return A[indexA + k - 1];

        if (k == 1) {
            return Math.min(A[indexA], B[indexB]);
        }
        int newK = k / 2 - 1;
        int midA = indexA + newK;
        int midB = indexB + newK;
        if (midA >= lenA) {
            return findKth(A, B, indexA, midB + 1, k - k / 2);
        } else if (midB >= lenB) {
            return findKth(A, B, midA + 1, indexB, k - k / 2);
        }
        int va = A[midA], vb = B[midB];
        if (va < vb) {
            return findKth(A, B, midA + 1, indexB, k - k / 2);
        } else
            return findKth(A, B, indexA, midB + 1, k - k / 2);

    }