Problem 382872 · hard · Level 03 Linear Management & Searching

The Middle Result Across Two Heats

binary search · partitioning · arrays

A swimming gala ran two heats, and each heat's integer results are already sorted in a and b; the two heats had at least one swimmer between them. Return the median of all the results from both heats as a float: the middle result when the total number of swimmers is odd, or the mean of the two middle results when it is even.

Examples

Input:  a = [2, 9], b = [5]
Output: 5.0

Input:  a = [1, 6], b = [3, 10]
Output: 4.5

Input:  a = [], b = [7]
Output: 7.0

Constraints

  • 0 <= len(a), len(b) <= 10**6, len(a) + len(b) >= 1
  • -10**9 <= a[i], b[i] <= 10**9
  • Aim for O(log(min(len(a), len(b)))) time. (Merging the lists still passes the tests; the point is to find the median without building the merged list.)

Goals

  • Binary search over a partition point of the shorter list
  • Use sentinels for partitions at the ends
  • Handle even and odd total lengths
Starting Python…