InferenceAI

Enter at least two characters to search.

Start free

Median of Two Sorted Lists

hardBinary Search40 min50 XP

Return the median of the combined values of two sorted lists a and b as a float. At least one list is non-empty.

Requirements

  • Run in O(log⁡min⁡(m,n))O(\log \min(m, n)) time by binary searching a partition.

Examples

EXAMPLE WALKTHROUGHodd total

A guided view of the published example, not a trace of your code.

INPUT[[1,3],[2]]
TASKmedian_of_two_sorted()
EXPECTED OUTPUT2

Start with the input values shown in this example.

Example 1 of 3

Input: a = [1,3], b = [2]

Output: 2

Explanation: odd total

Input: a = [1,2], b = [3,4]

Output: 2.5

Explanation: even total

Input: a = [], b = [1]

Output: 1

Explanation: one empty

Hint 1

Partition both lists so the left halves together hold half the elements.

Hint 2

A partition is correct when max(left parts) ≤ min(right parts).

Topics

DSABinary SearchPython