VOOZH about

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

⇱ Median of two Sorted Arrays of Different Sizes - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Median of two Sorted Arrays of Different Sizes

Last Updated : 3 Oct, 2025

Given two sorted arrays, a[] and b[], find the median of these sorted arrays. Assume that the two sorted arrays are merged and then median is selected from the combined array.

This is an extension of Median of two sorted arrays of equal size problem. Here we handle arrays of unequal size also.

Examples:

Input: a[] = [-5, 3, 6, 12, 15], b[] = [-12, -10, -6, -3, 4, 10]
Output: 3
Explanation: The merged array is [-12, -10, -6, -5 , -3, 3, 4, 6, 10, 12, 15]. So the median of the merged array is 3.

Input: a[] = [1], b[] = [2, 4, 5, 6, 7]
Output: 4.5
Explanation: The merged array is [1, 2, 4, 5, 6, 7]. The total number of elements are even, so there are two middle elements. Take the average between the two: (4 + 5) / 2 = 4.5

[Naive Approach] Using Sorting - O((n + m) × log (n + m)) Time and O(n + m) Space

The idea is to combines both sorted arrays into a new array and then sorts it. Once sorted, it finds the median by checking the total length. If the size is odd, it returns the middle element; if even, it returns the average of the two middle elements. This method is straightforward but not optimal in terms of time and space complexity.

Illustration:

a[] = [ -5, 3, 6, 12, 15 ], b[] = [ -12, -10, -6, -3, 4, 10 ]

  • After concatenating them in a third array: c[] = [ -5, 3, 6, 12, 15, -12, -10, -6, -3, 4, 10]
  • Sort c[] = [ -12, -10, -6, -5, -3, 3, 4, 6, 10, 12, 15 ]
  • As the length of c[] is odd, so the median is the middle element = 3

Output
3

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

The idea is to simulate the merging process of two sorted arrays without actually creating a new one. By iterating through both arrays together until reaching the middle index, the algorithm keeps track of the last two selected elements.

These are then used to compute the median: if the total number of elements is odd, the middle element is returned; if even, the average of the two middle elements is returned. This ensures correct handling for both even and odd combined lengths.


Output
3

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

Prerequisite: Median of two sorted arrays of same size

The approach is similar to the Binary Search approach of Median of two sorted arrays of same size with the only difference that here we apply binary search on the smaller array instead of a[].

  • Consider the first array is smaller. If first array is greater, then swap the arrays to make sure that the first array is smaller.
  • We mainly maintain two sets in this algorithm by doing binary search in the smaller array. Let mid1 be the partition of the smaller array. The first set contains elements from 0 to (mid1 - 1) from smaller array and mid2 = ((n+m+1) / 2 - mid1) elements from the greater array to make sure that the first set has exactly (n+m+1)/2 elements. The second set contains remaining half elements.
  • Our target is to find a point in both arrays such that all elements in the first set are smaller than all elements in the elements in the other set (set that contains elements from right side). For this we validate the partitions using the same way as we did in Median of two sorted arrays of same size.

Why do we apply Binary Search on the smaller array?

Applying Binary Search on the smaller array helps us in two ways:

  • Since we are applying binary search on the smaller array, we have optimized the time complexity of the algorithm from O(log n) to O(log(min(n, m)).
  • Also, if we don't apply the binary search on the smaller array, then then we need to set low = max(0, (n + m + 1)/2 - m) and high = min(n, (n + m + 1)/2) to avoid partitioning mid1 or mid2 outside a[] or b[] respectively.

To avoid handling such cases, we can simply binary search on the smaller array.


Output
3
Comment