Python Solution of Median of Two Sorted ArraysIn this problem, we will be given two arrays. Let the arrays be array1 and array2. These two arrays will be sorted and can have different sizes. Let the size of the two arrays be n and m where n is the number of elements in array1 and let m be the number of elements in array2. Let us see some examples to understand the problem. Input: array1 = [-5, 2, 7, 9, 11], array2 = [-13, -10, -6, 3, 6, 13] Output: The median of the two arrays is 3. If we will merge both the arrays the new array will be sorted = [-13, -10, -6, -5, 2, 3, 6, 7, 9, 11, 13] Since the number of elements is 11, i.e., an odd number of elements, median = (n + m - 1) / 2, the 6th element. Sorted[6] = 3 Input: array1 = [3, 6, 9, 10], array2 = [4, 7, 10, 11, 12, 13] Output: The median of two sorted arrays is 9.50 If we merge both the arrays, the new array will be sorted = [3, 4, 6, 7, 9, 10, 10, 11, 12, 13] Since the number of elements is 10, i.e., even number of elements, median = [(n + m) / 2, ((n + m ) / 2) + 1], we will take the average of these two values. Therefore median will be (sorted[5] + sorted[6]) / 2 = 9.50. Approach - 1The most basic approach to solve this problem is to create a third array. This third array will store the sum of two arrays; hence, the total size of this array will be n + m. We will sort this new array and find the median element. We must check if n + m is odd or even to find the median. As shown above, if the total length of the third array is odd, then the median is present at (n + m - 1) / 2 index of the sorted array. However, if the length of the third array is even, the median is the average of the two elements present at the indices (n + m) / 2 and ((n + m ) / 2) - 1 of the sorted array. The idea is very similar to the example shown above. We will follow these steps to solve the problem.
Now we will see the Python code for the above approach: Code Output: The sorted array is: [-13, -10, -6, -5, 2, 3, 6, 7, 9, 11, 13] The median of the sorted array is: 3 The sorted array is: [3, 4, 6, 7, 9, 10, 10, 11, 12, 13] The median of the sorted array is: 9.5 Time Complexity: We have to sort the third array of size (n + m). The time complexity will be O(Log (n + m)) to sort this array. Auxiliary Space: We are using extra memory space to store the third array, making the space complex O(n + m). Approach - 2We will use an efficient approach to sort the two arrays, decreasing the time complexity. The two arrays that are given to us here are sorted. We can use this property and merge the two arrays more efficiently instead of adding and sorting them later. We will maintain the count of the arrays that we have put in order, and when we reach half the total number of elements in both arrays, we will print the median element. In this approach, too, we have to deal with the two cases that appear based on the sum of the lengths of the two arrays. If the sum of lengths is odd, then the median is the value present in the (m + n) / 2 index after merging the two arrays. If the sum of lengths is even, then the median is the average of the values in the (m + n) / 2 and (m + n) / 2 + 1 indices after merging the two arrays. Let us have a dry run to understand this approach.
Below is the Python code for this approach. Code Output: The median of the sorted array is: 3 The median of the sorted array is: 9.5 Time Complexity: We need to sort the merged array in this approach. We have found the median using a single for loop. Therefore, the time complexity of this approach is O(m + n). Auxiliary Space: Unlike the previous approach, we have not stored anything where we stored merged arrays. Therefore, the space complexity of this approach is constant, i.e., O(1). Approach - 3We will use binary search in this approach. As we saw in the previous approach, the property that the given two arrays are sorted can be utilized to design an efficient approach to find the median of two arrays. One of the most commonly used approaches for sorted arrays is binary search. We will use binary search to divide the two arrays without explicitly merging and sorting them. We must find a point that can divide the merged array into two equal parts to find the median. We can use binary search to find a point in both the arrays individually. When we merge the two arrays, those points will merge too and will be the point that divides the merged array into two equal parts. Let us take an example: array1 = [-5, 2, 7, 9, 11], array2 = [-13, -10, -6, 3, 6, 13]
Now, we will see the steps we must follow to solve this problem using Python.
Code Output: The median of the sorted array is: 3 The median of the sorted array is: 9.5 Time Complexity: We are applying binary search on the array with greater length. Therefore, the time complexity will be O(max(log m, log n)). We applied this condition to pass the cases where the length of one of them is 0. Auxiliary Space: We have not used extra space to store the array. Therefore, the space complexity is constant, hence, O(1). Approach - 4In this approach, we will use Priority Queue min Heap to find the median value. We will find the median value using the priority queue by iterating over the arrays until we reach the median index. Let us understand the algorithm with the help of an illustration. array1 = [-5, 2, 7, 9, 11], array2 = [-13, -10, -6, 3, 6, 13], n = 5, m = 6 (n = length of array1, m = length of array2) Step 1: We will initialize a priority queue p_q. We will add the elements of the first array to the priority queue. p_q.push(-5) p_q.push(2) p_q.push(7) p_q.push(9) p_q.push(11) When we add all the first array elements to the priority queue, the final queue will be [-5, 2, 7, 9, 11]. Step 2: Now we will add the elements of the second array to the priority queue. p_q.push(-13) p_q.push(-10) p_q.push(-6) p_q.push(3) p_q.push(6) p_q.push(13) When we add the elements of the second array to the priority queue, the final queue will be p_q = [-13, -10, -6, -5, 2, 3, 6, 7, 9, 11, 13] Step 3: The final step is to find the median value. We will find the median using approaches based on the condition that the priority queue length is even or odd. If the length of the priority queue is odd Then, we will iterate over the queue for (n + m) // 2 number of times. In each iteration, we will pop an element from the queue. At the end of the iteration, the top element of the queue will be the median. If the length of the priority queue is even Then we will iterate over the priority queue upto (n + m) // 2 and ((n + m) // 2) - 1. We will return the average of the top elements of the queue. Here is the Python code for this approach. Code Output: The median of the sorted array is: 3 The median of the sorted array is: 9.5 Time Complexity: Here, the time complexity will be O(max(n, m) * log(max(n, m))). The time complexity is this because we used a priority queue for both arrays. Auxiliary Space: We have used extra space to store the priority queue. Hence, the space complexity will equal the length of the priority queue, i.e., O(n + m). Next TopicThe Maximum Sum Subarray Problem |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India