Skip to content
BASA DSA

Median of Two Sorted Arrays

HardBinary Search

Return the median of the multiset formed by two sorted arrays. For even length average the two middle values. Aim for logarithmic time in the combined length.

Example 1

Input
nums1 = [1,5]
nums2 = [2,3]
Output
2.5

Explanation

Merged order is 1,2,3,5; average the middle values to get 2.5.

Example 2

Input
nums1 = []
nums2 = [4]
Output
4

Explanation

The nonempty array alone supplies the median 4.

Example 3

Input
nums1 = [-2,0]
nums2 = [0,8]
Output
0

Explanation

Merged order is -2,0,0,8, with both middle values zero.

Constraints

  • 0 <= each array length <= 1000
  • Combined length is positive
  • Values are -1000000..1000000

Function contractfindMedianSortedArraysArguments: json, json
Loading draft…
SolutionRuns on your device
Loading draft…