15% off all plans & roadmaps Use code
See plansLeetCode 4 · Solution & editorial
Given two sorted arrays nums1 and nums2 of size m and n respectively, return the median of the two sorted arrays.
Median of Two Sorted Arrays is a Binary Search Patterns problem, in the Median and Kth of Two Sorted Arrays subpattern. The pattern covers standard binary search, rotated arrays, first/last occurrence, and search on answer space
Master the binary search approach to find median of two sorted arrays in O(log(min(m,n))) time.
The reference solution runs in O(log(min(m, n))) time and O(1) space.
Median of Two Sorted Arrays has been reported in interviews at Amazon, Goldman Sachs, Facebook, Microsoft, Apple, Adobe, Google, Bloomberg.
The full editorial is part of Pro
It explains the core insight, the complexity analysis and the common mistakes, with a worked solution.
Median of Two Sorted Arrays is a Binary Search Patterns problem, in the Median and Kth of Two Sorted Arrays subpattern. Once the approach above makes sense, write it yourself and then work through the neighbouring problems that reuse it.