There is already an answer for two sorted arrays. However, in my question one of the arrays is unsorted.
Suppose X[1..n]
and Y[1..m]
where n < m
. X is sorted and Y is unsorted. What is the efficient algorithm to find the kth smallest number of X U Y
.
MinHeap can be used to find the kth smallest number in an unsorted array. However, here one of these arrays is sorted. I can think of:
1. Building a `MinHeap` for `Y`
2. i = 1, j = 1
3. x1 = extract Min from Y
4. x2 = X[i];
5. if j == k: return min(x1, x2)
5. if x1 < x2: j++; goto 3
6. else: j++; i++; goto 4
Is it efficient and correct?