if A[i] <= B[j]:
    if j == 0 or B[j - 1] <= A[i]:
        return A[i]
return kthsmallest(A[i:], B[:j], k - i)
if i == 0 or A[i - 1] <= B[j]:
    return B[j]
return kthsmallest(A[:i], B[j:], k - j)
Finding the kth-smallest element in union of sorted arrays