Two sorted array. Find kth smallest element, 要求O(logK)
Two sorted array. Find kth smallest element
O(logK)
A:
Compare A[k/2] and B[k/2].
If A[k/2] < B[k/2], it means these A[1] to A[k/2] elements are part of first k elements.
Now, the kth element can be in A[k/2 + 1] to A[k] or B[1] to B[k/2]. (note: here may be B[1] to B[k], *not* B[k/2])
Hence, we reduced the problem to half its original size.
Repeating the comparison of mid-elements of this subproblem, we reduce it to further half.
Hence at the end of logK iterations, the lesser value will be the kth element.
So, complexity is O( log K)