Median of two sorted arrays
Return the median of two sorted numeric lists whose combined length is positive, using O(log(min(m,n)+1)) comparisons and constant extra space.
a, b = [1, 2], [3, 4]
left_values, right_values = [a[-1]], [b[0]]
print((max(left_values)+min(right_values))/2)Reason about the state
Binary-search a partition in the shorter list. The left halves must contain half the items and every left boundary must be no larger than the opposite right boundary. Before writing code, identify what each variable means after one iteration. Walk through the example and explain why the next transition preserves that meaning. Then check the smallest permitted input and a case with repeated values. A correct answer follows the contract for every input, including the boundary cases in the checks. Keep any input mutation within the stated rules; return a fresh result when the task asks for one.
Your task
Return the median of two sorted numeric lists whose combined length is positive, using O(log(min(m,n)+1)) comparisons and constant extra space.
The explanation opens here once every check passes.