Menu
CoddyTech

Median of Two Sorted Arrays

You get two lists of integers, nums1 and nums2, each sorted from smallest to largest and holding at least one value. Return the median of all their values together, as text.

For an odd total count the median is the middle value, such as "4". For an even count it is the average of the two middle values, such as "2.5" or "-0.5". Write a whole number without a decimal point, and zero as "0", never "-0". Aim for O(log(min(m, n))) time.

Function

findMedianSortedArrays(nums1: integer-array, nums2: integer-array) → string
nums1integer-array
the first list, sorted from smallest to largest
nums2integer-array
the second list, sorted from smallest to largest
Returnsstring
the median of all the values, as text such as "4" or "-2.5"

Constraints

  • 1 ≤ nums1.length, nums2.length ≤ 3000
  • -106 ≤ nums1[i], nums2[i] ≤ 106
  • Both lists are sorted in non-decreasing order, and values may repeat.

Examples

Input
nums1 = [1, 4]nums2 = [2, 7, 9]
Output
"4"
Explanation
Together the values are 1, 2, 4, 7, 9. Five values have one middle value, the third, which is 4.
challenge icon

Follow-up

Can you use the same cut to find the k-th smallest value of the two lists, for any k, in logarithmic time?

Reset code
def findMedianSortedArrays(nums1, nums2):
    # Write code here

Test #1

Test #2

Test #3

Arguments

nums1 = [1, 4]
nums2 = [2, 7, 9]

Expected Output

"4"