Find Median of Two Sorted Arrays Using Binary Search in C++



The median is defined as the middle value of a sorted list of numbers, and the middle value is found by ordering the numbers in ascending order. Once the numbers are ordered, the middle value is called the median of the given data set.

Here, in this article, we have two different sorted arrays and need to find the median of these two array using binary search.

Median depends on the sorted combined array. So, the following cases may occur:

  • If the length of the combined array is odd, then the median should be the middle element of the array.
  • If the length of the combined array is even, there will be two middle elements. So, the average of both middle elements will be the median of the array.

Input / Output Scenario

The following are the input/output scenario:

Input: arr1[] = [-1, 0, 3, 4, 5], arr2[] = [-2, 5, 7, 8, 9, 10]
Output: 5
Explanation: The combined array is [-2, -1, 0, 3 , 4, 5, 5, 7, 8, 9, 10]. So the median of the combined array is 5.

Input: a[] = [3, 7], b[] = [2, 4, 5, 6]
Output: 4.5
Explanation: The combined array is [2, 3, 4, 5, 6, 7]. The total number of elements is even, so there are two middle elements. Average of these two is: (4 + 5) / 2 = 4.5

Find Median Using Binary Search

A binary search is an efficient algorithm for finding an item in a sorted array. It works by repeatedly dividing by half the portion of the array.

Following is the steps:

  • Calculate the length of the subarrays using: length = e1 - s1 + 1 (since indices are inclusive).
  • Base Cases:
  • If length is 1, return the median directly based on array values.
  • If length is 2, return the median of the four values available.
  • Find individual medians for both arrays and check:
  • If both medians are equal, that is the median of combined arrays.
  • Adjust search space:
  • If m1 > m2, search in the first half of array1 and second half of array2.
  • If m1 < m2, search in the second half of array1 and first half of array2.
  • Recursively call median() with updated index ranges.

C++ program to find the median of two sorted arrays using binary search approach

In the following example, we find the median of two sorted arrays using the binary search:

#include<iostream>
using namespace std;

void median(float a1[], int s1, int e1, float a2[], int s2, int e2) {
   float m1, m2;
   if ((e1 - s1 + 1) % 2 == 0) {
      if (e1 - s1 == 1) {
         m1 = ((a1[s1] < a2[s2] ? a1[s1] : a2[s2]) + (a1[e1] > a2[e2] ? a1[e1] : a2[e2])) / 2;
         cout << m1;
         return;
      }
      m1 = (a1[(e1 + s1) / 2] + a1[(e1 + s1) / 2 + 1]) / 2;
      m2 = (a2[(e2 + s2) / 2] + a2[(e2 + s2) / 2 + 1]) / 2;
      if (m1 == m2) {
         cout << m1;
         return;
      } else {
         if (m1 > m2)
            median(a1, s1, (e1 + s1) / 2 + 1, a2, (e2 + s2) / 2, e2);
         else
            median(a1, (e1 + s1) / 2, e1, a2, s2, (e2 + s2) / 2 + 1);
      }
   } else {
      if (e1 - s1 == 0) {
         m1 = (a1[s1] + a2[s2]) / 2;
         cout << m1;
         return;
      }
      m1 = a1[(e1 + s1) / 2];
      m2 = a2[(e2 + s2) / 2];
      if (m1 == m2) {
         cout << m1;
         return;
      } else {
         if (m1 > m2)
            median(a1, s1, (e1 + s1) / 2, a2, (e2 + s2) / 2, e2);
         else
            median(a1, (e1 + s1) / 2, e1, a2, s2, (e2 + s2) / 2);
      }
   }
   return;
}

int main() {
   float a1[] = {1, 3, 5, 7, 9};
   float a2[] = {2, 4, 6, 8, 10};
   int n1 = sizeof(a1) / sizeof(a1[0]);
   int n2 = sizeof(a2) / sizeof(a2[0]);

   cout << "Median is ";
   median(a1, 0, n1 - 1, a2, 0, n2 - 1);

   return 0;
}

This is the median of the above code:

Median is 5.5
Updated on: 2025-05-20T18:47:53+05:30

186 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements