VOOZH about

URL: https://www.geeksforgeeks.org/dsa/median-of-two-sorted-arrays/

⇱ Median of two sorted arrays of same size - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Median of two sorted arrays of same size

Last Updated : 29 Oct, 2025

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 = 16

Input: 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

[Naive Approach] Using Sorting - O(n * logn) Time and O(n) Space

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

Output
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.

[Better Approach] Using Merge of Merge Sort - O(n) Time and O(1) Space

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.


Output
16

Time Complexity: O(n), where n is the size of array a[] and b[].
Auxiliary Space: O(1)

[Expected Approach] Using Binary Search - O(log n) Time and O(1) Space

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].

How to check if the partition mid1 and mid2 is valid or not?

For mid1 and mid2 to be valid, we need to check for the following conditions:

  • All elements in a[0...mid1 - 1] should be less than or equal to all elements in b[mid2...n - 1]. Since both the subarrays are sorted individually, we can check a[mid1 - 1] should be less than or equal to b[mid2].
  • All elements in b[0...mid2 - 1] should be less than or equal to all elements in a[mid1...n - 1]. Since both the subarrays are sorted individually, we can check b[mid2 - 1] should be less than or equal to a[mid1].

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:

  • If l1 > r2, this means that we have taken extra elements from a[], so take less elements by moving high = mid - 1.
  • If l2 > r1, this means that we have taken less elements from a[], so take more elements by moving low = mid + 1.

Illustration:


Below is the implementation of the above approach:


Output
16

Time Complexity: O(log n), where n is the size of input array.
Auxiliary Space: O(1)

Related Articles:

Comment