v9.1.1
Menu

Median of Two Sorted Arrays

hardFREESearch & SortGoogle DSA

Given two sorted arrays nums1 and nums2, return the median of the two sorted arrays.

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

Output the median as a float with one decimal place.

Example

Input
1,3
2
Output
2.0
Reasoning
  • The input arrays are nums1 = [1, 3] and nums2 = [2].
  • We merge the two sorted arrays to get a single sorted array: [1, 2, 3].
  • The length of the merged array is 33, which is odd, so the median is the middle element: 22.
  • The final output is the median as a float with one decimal place: 2.02.0

Constraints

  • 0 <= len(nums1), len(nums2) <= 1000
  • -10^6 <= nums1[i], nums2[i] <= 10^6
  • At least one array is non-empty

Learn this in

More from Google DSA

solution.py

Test results

Sign in to run your code against the test cases.