← All problems

Median of Two Sorted Arrays

Hard searchingarrays LeetCode ↗
Given two sorted arrays, find the median of the combined sorted array in O(log(m+n)) time. The naive merge is O(m+n); the optimal solution partitions both arrays.

Hints (0/3 revealed)

Log in to reveal hints progressively.

Complexity

Time O(log(min(m,n))), Space O(1)

Common pitfalls

  • Merging (O(m+n) - too slow per spec)
  • Odd/even total confusion

Your solution

Log in to save your code and track progress.