Paths Subjects Questions Quizzes Pricing Search
Advanced Open Pro

Find the Median From Two Sorted Arrays

Given two sorted arrays nums1 and nums2 of sizes m and n respectively, return the median of the combined, sorted set of all m + n elements.

Your solution should run in O(log(min(m, n))) time.

Example 1

Input: nums1 = [1, 3], nums2 = [2]
Output: 2.0
Explanation: The merged array is [1, 2, 3], whose median is 2.

Example 2

Input: nums1 = [1, 2], nums2 = [3, 4]
Output: 2.5
Explanation: The merged array is [1, 2, 3, 4]. The median is the
average of the two middle values, (2 + 3) / 2 = 2.5.

Constraints

  • 0 <= m, n <= 1000, 1 <= m + n <= 2000
  • -10^6 <= nums1[i], nums2[i] <= 10^6
  • Both arrays are sorted in ascending order.

Share this question

← Back to Binary Search practice

We use cookies for product analytics to improve OmniAtlas. See our Privacy Policy.