AlgoLounge

Median of Two Sorted Arrays

hard

Given two sorted arrays nums1 and nums2 of size m and n respectively, return the median of the two sorted arrays.

The overall run time complexity should be O(log (m+n)).

Examples

Example 1

Input:  nums1 = [1,3], nums2 = [2]
Output: 2.00000

merged array = [1,2,3] and median is 2.

Example 2

Input:  nums1 = [1,2], nums2 = [3,4]
Output: 2.50000

merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5.

Constraints

  • nums1.length == m
  • nums2.length == n
  • 0 <= m <= 1000
  • 0 <= n <= 1000
  • 1 <= m + n <= 2000
  • -10^6 <= nums1[i], nums2[i] <= 10^6
TypeScript
Loading editor…
List of test cases (10)
Case 1
nums1 = [1,3]
nums2 = [2]
Case 2
nums1 = [1,2]
nums2 = [3,4]
Case 3
nums1 = [0,0]
nums2 = [0,0]
Case 4
nums1 = []
nums2 = [1]
Case 5
nums1 = [2]
nums2 = []
Case 6
nums1 = [1,2]
nums2 = [3,4]
Case 7
nums1 = []
nums2 = [2,3]
Case 8
nums1 = [1]
nums2 = [2,3,4]
Case 9
nums1 = [1,2,3]
nums2 = [4,5,6]
Case 10
nums1 = [1,3,8]
nums2 = [7,9,10,11]