Skip to content
RED SAGSkills

Median of Two Sorted Arrays

Two services each report a latency list, a and b, already sorted ascending. Return the median of all values combined. If the combined count is even, the median is the average of the two middle values.

Target O(log(min(m, n))) time — do not merge the arrays.

Example 1

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

Example 2

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

Constraints

  • 0 <= a.length, b.length <= 10^5
  • 1 <= a.length + b.length
  • -10^6 <= a[i], b[i] <= 10^6
Hint 1

Binary search on how many elements of the shorter array go into the left half of the combined order.

Hint 2

A split is correct when every left-side value is <= every right-side value.

Good practice for: Data Scientist, Data Engineer, Machine Learning Engineer, AI Engineer (LLM & Generative AI), Backend Developer, Full Stack Developer, Site Reliability Engineer

Run checks your code on 2 sample tests. Submit runs all 9 tests, including hidden ones, and earns points. Use console.log to debug. Python's first run can take a few seconds while it starts.