#4

Median of Two Sorted Arrays

candidate master · 1540 · lc hard +32 · verified · 46% accepted · 31,900 likes · top 30%

play →

Description

Two sorted integer arrays nums1 and nums2 have lengths m and n. Find and return the median of the two arrays merged. The algorithm must run in O(log (m+n)) time.

Example 1:

Input: nums1 = [1,3], nums2 = [2]
Output: 2.00000
Explanation: merged array = [1,2,3] and median is 2.

Example 2:

Input: nums1 = [1,2], nums2 = [3,4]
Output: 2.50000
Explanation: merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5.

Code

1
2
3