We remark that for Ai < Bj , it must be true that Ai < Bj-1 . On the other hand, if Bj < Ai , then Bj < Ai-1 .. How can this be true for any i and j ?
This is not true for all pairs i and j . The article deals with a special situation.
Firstly, it is assumed that there are no duplicates, even in the form of common elements A and B Secondly, the conclusion that
Ai < Bj ==> Ai < Bj-1, resp. Bj < Ai ==> Bj < Ai-1
provided that neither
Bj-1 < Ai < Bj resp. Ai-1 < Bj < Ai
takes place. So, excluding these configurations, Ai < Bj ==> Ai <= Bj-1 and Bj < Ai ==> Bj <= Ai-1 follow immediately, and strict inequalities follow from the assumption that there are no duplicates.
We are trying to approach this complex problem by comparing the middle elements A and B, which we identify as Ai and Bj. If Ai is between Bj and Bj-1, we just found i + j + 1 smallest element
In array B there are j elements less than Bj , and in array A there are i elements less than Ai (indices start at 0). Therefore, if Bj-1 < Ai < Bj , both arrays together contain exactly j + i elements smaller than Ai .
What will change if there are duplicates?
Little.
We are still considering a situation where i + j = k-1 . Suppose that Ai <= Bj .
- What if
Ai = Bj ? - What if
Ai < Bj ?
In case 1. let m be the smallest index such that Am = Ai and n smallest index such that Bn = Bj . Then in both arrays together there are exactly m + n <= i + j = k-1 elements strictly smaller than Ai , and at least (i+1) + (j+1) = (k+1) elements, not exceeding Ai . Therefore, the kth smallest element is equal to Ai .
For 2. we consider three cases: a) Bj-1 < Ai , b) Bj-1 = Ai , c) Bj-1 > Ai .
In case a) we have j elements in B that are no more than Ai , and all of them are strictly less, and elements m <= i in A that are strictly less than Ai ( m , as indicated above) and an odd number, but not less than i-m+1 elements equal to Ai . Thus, in both arrays exactly j + m <= j + i = k-1 elements that are strictly less than Ai and at least j + m + (i-m+1) = j+i+1 = k elements exceeding Ai ; therefore, the kth smallest element of both arrays together is equal to Ai .
In case b), the same argument shows that the kth smallest element of both arrays together is equal to Ai .
In the remaining case, Ai < Bj-1 situation becomes a little more complicated. Array B contains at least j elements no more than Bj-1 , and array A contains at least i+1 elements strictly smaller than Bj-1 , so the kth smallest element of both arrays together is in most of Bj-1 . But it cannot be less than Ai ( B contains no more than j-1 elements less than Ai , therefore both arrays together contain no more than i + (j-1) = k-2 elements less than Ai ).
Thus, we can discard the part below Ai from array A and the part above Bj-1 from array B and act as without duplicates.
All that changed was that several strict inequalities had to be replaced by weak inequalities.
Code (would be more efficient if starting indexes and lengths were passed instead of slicing, but slicing gives a shorter code):
def kthsmallest(A, B, k): if k < 1: return None a_len, b_len = len(A), len(B) if a_len == 0: return B[k-1]