Given a unimodal array A of n distinct elements (meaning that its entries are in increasing order up until its maximum element, after which its elements are in decreasing order), an integer p (that is the length of the increasing first part) and k (the k-th minimum element) Give an algorithm to compute the value of the kth minimal element that runs in O(log n) time.
Example:
A= {1,23,50,30,20,2}
p= 2
k=3
Answer: 20
Edit
I tried this:
def ksmallest(arr1, arr2, k):
if len(arr1) == 0:
return arr2[len(arr2)-k-1]
elif len(arr2) == 0:
return arr1[k]
mida1 = (int)(len(arr1)/2)
mida2 = (int)((len(arr2)-1)/2)
if mida1+mida2<k:
if arr1[mida1]>arr2[mida2]:
return ksmallest(arr1, arr2[:mida2], k-(len(arr2)-mida2))
else:
return ksmallest(arr1[mida1+1:], arr2, k-mida1-1)
else:
if arr1[mida1]>arr2[mida2]:
return ksmallest(arr1[:mida1], arr2, k)
else:
return ksmallest(arr1, arr2[mida2+1:], k)