Trim both sizes k. If necessary, ask the program to imagine infinity at the end of one or both arrays to bring them to size k; this will not affect the asymptotic runtime. (In a real implementation, we are likely to do something more efficient.)
k/2'- . , k- ; k/2'th A, - B. A B, k/2- , . , k = 1.
A , B . K/2- , , , A, k- .
, Python:
def kth(array1, array2, k):
if k == 1:
return min(array1[0], array2[0])
mid = k//2 - 1
if array1[mid] > array2[mid]:
array1, array2 = array2, array1
return kth(array1[k//2:], array2[:k//2], k//2)
, .