Back to DSA

Median of Two Sorted Arrays

hard
Acceptance: 37%
ArraysTwo Pointers

Two individually sorted arrays of possibly different lengths are provided. Determine the median of the combined collection of elements. The algorithm must run in O(log(m+n)) time, where m and n are the sizes of the two arrays.

Examples

Example 1:
Input:a = [1,4,6], b = [2,3,5]
Output:3.5
Explanation: The merged sequence is [1,2,3,4,5,6]. The median is the average of positions 3 and 4: (3+4)/2 = 3.5.
Example 2:
Input:a = [1,2], b = [3]
Output:2.0
Explanation: Merged: [1,2,3]. The middle element is 2.

Hints

00:00
1234567