Yes, if one array is empty, the median is simply the median of the non-empty array, which the algorithm handles as a special case.
Median of two sorted array in Python O(log n)
The median of two sorted arrays is a fundamental problem that requires finding the middle value of the combined elements in the arrays. In this Answer, we will discuss an efficient algorithm to solve this problem with the time complexity of
Key takeaways:
The median of two sorted arrays can be found in
time using binary search, rather than the typical time as in case of merging the arrays approach. If the array’s combined length is odd, the median is the middle element, whereas in the case of even length, the median is the average of the two middle elements.
The solution involves finding partition points in both arrays where elements on the left are less than or equal to those on the right, ensuring that median calculations are accurate.
Understanding the problem
Given two sorted arrays, array1 and array2, with lengths m and n respectively, our task is to find the median of the combined array that contains all elements from both array1 and array2. The median is defined as follows:
If the total number of elements (m + n) is odd, the median is the middle element.
If the total number of elements is even, the median is the average of the two middle elements.
Binary search approach
To achieve
Finding the partition points
For an array with n elements, the partition point can be represented by an index i, which is the midpoint of the smaller array initially. To find the partition points, we will apply binary search on the smaller of the two arrays, array1. Once we have the partition point i for array1 using the formula: j for array2 using the formula:
In the context of the binary search algorithm, left and right represent the search bounds within the smaller array. left starts at left contains half of the elements from both arrays, and the right contains the other half. As the search narrows, i (the partition index for the smaller array) is updated, and the left and right help converge on the correct partition.
Handling edge cases
We need to handle some edge cases, such as when i is 0 or m, or when j is 0 or n. These cases indicate that there are no elements on one side of the partition, and we handle them accordingly.
Calculating the median
Finally, we calculate the median using the partition points i and j.
If the total number of elements
is odd, the median is the maximum elements in the left partition . If the total number of elements is even, the median is the average of the maximum of the left partitions and the minimum of the right partitions
.
Example
Code example
Let’s look at the code for the algorithm we just discussed:
Educative offers an AI-Powered Mock Interviewer designed to cater to users of all levels, from beginners to intermediate and expert audiences.
Code explanation
Line 3–4: To make binary search faster, we swap the arrays if
array1is larger, ensuring thatarray1is always the smaller array. This minimizes the range of binary search.Line 6–8: Initialize the following variables:
mandnstore the lengths ofarray1andarray2, respectively.half_lencalculates half the combined length of both arrays. This helps to split the arrays to find the median.leftandrightdefine the search range for binary search overarray1.
Line 10–12: The loop performs binary search on
array1.iis the midpoint of the current binary search range inarray1.jis the corresponding position inarray2such that the left half and right half together make up half the total length (half_len).
Line 14–15: If
iis withinarray1bounds and the element atarray2[j - 1]is greater thanarray1[i],iis too small. We incrementleftto search the right half.Line 16–17: If
iis greater than zero andarray1[i - 1]is greater thanarray2[j],iis too large. We decrementrightto search the left half.Line 18–24: If neither of the above conditions is met,
iis positioned correctly.max_of_leftwill contain the maximum value of the left half.If
i == 0, it means there are no elements left inarray1on the left, so we takearray2[j - 1].If
j == 0, it means there are no elements left inarray2on the left, so we takearray1[i - 1].Otherwise,
max_of_leftis the maximum ofarray1[i - 1]andarray2[j - 1].
Line 26–27: If the total length
(m + n)is odd, the median ismax_of_left, as there’s a single middle element.Line 29–34: If
(m + n)is even, the median is the average of the two middle values.min_of_rightfinds the minimum of the right half:If
i == m, it means all elements inarray1are on the left, so we takearray2[j].If
j == n, it means all elements inarray2are on the left, so we takearray1[i].Otherwise,
min_of_rightis the minimum ofarray1[i]andarray2[j].
Line 37: Finally, for even-length arrays, we return the average of
max_of_leftandmin_of_rightas the median.
In case of arrays with different length, the time complexity of this code is O(log min(m,n)), where m and n are the lengths of array1 and array2, respectively. This complexity arises due to the binary search only operating on the smaller of the two arrays, aiming to minimize search operations.
Space complexity
The space complexity of the solution is
Conclusion
Finding the median of two sorted arrays is an important problem that can be solved efficiently with the time complexity of
Frequently asked questions
Haven’t found what you were looking for? Contact Us
Can this algorithm handle cases where one array is empty?
How does the algorithm determine if the median is a single middle value or an average?
Why is binary search applied on the smaller array instead of both?
What happens if the arrays are of very different lengths?
Free Resources