from bisect import bisect_right

def kth_element(arrs, k):
    low = min(arr[0] for arr in arrs if len(arr))
    high = max(arr[-1] for arr in arrs if len(arr))

    while low < high:
        mid = low + (high - low) // 2
        cnt = 0
        for arr in arrs:
            cnt += bisect_right(arr, mid)
        if cnt >= k:
            high = mid
        else:
            low = mid + 1
    return low

def solve(arrs):
    tot = sum(len(arr) for arr in arrs)

    if tot % 2:
        m = (tot + 1) // 2
        return kth_element(arrs, m)
    else:
        m = tot // 2
        return (kth_element(arrs, m) + kth_element(arrs, m + 1)) / 2

tests = (
    ([[5,7,9],[7,10,11,13]], 9),
    ([[10,11,12],[14,20,40]], 13),
    ([[20,30,40,50,60],[70,80],[90,100]], 60),
    ([[1,8,10],[2,3,4],[12,20]], 6),
    ([[],[2,3,4]], 3),
    ([[10,15,20],[22,26,30]], 21)  
)
for arr, sol in tests:
    print("Input :", arr, "\nYour algorithm :", solve(arr), "\nExpected value :", sol, '\n', "#" * 20)

Embed on website

To embed this project on your website, copy the following code and paste it into your website's HTML: