![]() |
VOOZH | about |
Given 2 sorted arrays a[] and b[], each of size n, the task is to find the median of the array obtained after merging a[] and b[].
Note: Since the size of the merged array will always be even, the median will be the average of the middle two numbers.
Input: a[] = [1, 12, 15, 26, 38], b[] = [2, 13, 17, 30, 45]
Output: 16
Explanation: The merged sorted array is [1, 2, 12, 13, 15, 17, 26, 30, 38, 45]. The middle two elements are 15 and 17, so median = (15 + 17)/2 = 16Input: a[] = [10], b[] = [21]
Output : 15.5
Explanation : The merged sorted array is [10, 21]. The middle two elements are 10 and 21, so median = (10 + 21)/2 = 15.5
Table of Content
The idea is to concatenate both the arrays into a new array, sort the new array and return the middle of the new sorted array.
Illustration:
a[] = [ 1, 12, 15, 26, 38 ], b[] = [ 2, 13, 17, 30, 45]
- After concatenating them in a third array : c[] = [ 1, 12, 15, 26, 38, 2, 13, 17, 30, 45]
- Sort c[] = [ 1, 2, 12, 13, 15, 17, 26, 30, 38, 45 ]
- So the median is the average of two middle elements: (15 + 17) / 2 = 16
16
Time Complexity: O((2n) * log(2n)), where n is the size of array a[] and b[].
Auxiliary Space: O(2n), because we are creating a new merged array of size 2n.
The given arrays are sorted, so merge the sorted arrays in an efficient way and keep the count of elements processed so far. So when we reach half of the total, print the median. The median will be the average of elements at index (n - 1) and n in the array obtained after merging both the arrays.
16
Time Complexity: O(n), where n is the size of array a[] and b[].
Auxiliary Space: O(1)
To find the median of the two sorted arrays, a[] and b[] of size n, we need the average of two middle elements of merged sorted array. So, if we divide the merged array into two halves, then the median will be (last element in first half + first element in second half) / 2.
The idea is to use Binary Search to find the valid partition in a[] say mid1, such that all elements of a[0...mid1 - 1] will lie in the first half of the merged sorted array. Since, we know that first half of the merged sorted array will have total n elements, the remaining mid2 = (n - mid1) elements will be from b[]. In other words, the first half of the merged sorted array will have all the elements in a[0...mid1 - 1] and b[0...mid2 - 1].
For mid1 and mid2 to be valid, we need to check for the following conditions:
For simplicity, take the element to the left of partition mid1 as l1, so l1 = a[mid1 - 1] and element to the right of partition mid1 as r1, so r1 = a[mid1]. Similarly, take the element to the left of mid2 as l2, so l2 = b[mid2 - 1] and element to the right of mid2 as r2, so r2 = b[mid2]. So, the above conditions can be simplified as l1 <= r2 and l2 <= r1.
If the partition is not valid, we can have two cases:
Illustration:
Below is the implementation of the above approach:
16
Time Complexity: O(log n), where n is the size of input array.
Auxiliary Space: O(1)
Related Articles: