Median of two Sorted Arrays of Different Sizes using Binary Search

Given two sorted arrays, a[] and b[] os size n and m, the task is to find the median of these sorted arrays, in O(log(min(n, m)), when n is the number of elements in the first array, and m is the number of elements in the second array.
Note: In case of even numbers in total and if we want to return a median that exists in the merged array we can return the element in the (n+m)/2 or ((n+m)/2 β 1)th position.
Examples:
Input: a[] = {-5, 3, 6, 12, 15}, b[] = {-12, -10, -6, -3, 4, 10}
Output : The median is 3.
Explanation: The merged array is {-12, -10, -6, -5 , -3, 3, 4, 6, 10, 12, 15}, with median 3Input : a[] = {2, 3, 5, 8}, b[] = {10, 12, 14, 16, 18, 20}
Output : The median is 11.
Explanation: The merged array is {2, 3, 5, 8, 10, 12, 14, 16, 18, 20}
Median = average of two middle elements (as number of the elements are even) = (10 + 12) / 2 = 11.
Binary Search Approach to find Median of Two Sorted Arrays of Different Sizes:
Let us consider the case for a total of odd number of elements. Also consider, that i elements from the first array and j elements from the 2nd array are on the left half of the median (including median itself). Then the maximum of a[i] and b[j] is the median, The important task is to efficiently find out such i and j.
How to efficiently find i and j?Β
Let us consider there are i elements from the first array that are on the left of the median and the other elements on the left are from the second array. So the number of other elements are j = (n + m + 1)/2 β i. Now we need to validate if our assumption is correct. For that we have take the following actions:
- If a[i+1] > b[j] and b[j+1] > a[i] then we can definitely say that these are the elements on the left of the median.
- Otherwise, we have to reduce the number of elements taken from any one array. That can be decided based on the following fact:
- If a[i+1] is less than b[j], then we need to consider more elements from the first array.
- Otherwise, if b[j+1] is less than a[i], we need to consider more elements from the second array.
Instead of linear incrementation for considering more elements from any of the array, we can use Binary Search here.
Consider, in a previous search we have found x1 elements must be considered from the first array, and we consider not more than x2 elements can be chose from the first array. Now we are checking for i = (x1 + x2)/2 elements from the first array.
- If a[i+1] < b[j], we will change x1 = i+1.
- If the reverse is true, i.e., b[j+1] < a[i] make x2 = i-1.
So the solution comes down to the following:
Initially take x1 = 0 and x2 = N and repeatedly follow the above binary search concept repeatedly, till we find out such i and j. After that the maximum between a[i] and b[j] will be the median when there are total odd number of elements.
Note that in case the total number of elements is even, the median will be the average of max(a[i], b[j]) and min(a[i+1], b[j+1]).
Note: The partition didnβt work if any one array is empty from the given arrays:
For example: if arr1=[2], arr2=[] by this Β β(n + m + 1) / 2β formula the value of i=0 and value of j=1 and this give you out of index error because arr2 is empty and arr2[j] give you out of index error. You have to handle this case by checking if one array is empty you can simply return the medium of another array.
What to be careful about during calculation?
There is one scope of error while calculating j = (n + m + 1)/2 β i. This may result in a negative value of j for some cases. So to avoid accessing a negative index, check the value of j, and if it is negative, the median is on the first array itself. Find the element at the position (n + m + 1)/2 [the average of (n+m)/2 and the next element if total even number of elements] and return this as the median.
Illustration of finding Median of two sorted arrays of different size using Binary Search
See the below examples for a better understanding:
Example 1: When the total number of elements is odd:
Step 1: Initially, consider that 3 elements from the first array are to the left of the median. But this does not fit the condition as 20 > 11. So, for the next iteration, a minimum of 4 elements from the first array should be considered.
Consider 3 elements from 1st array on the left of the median
Step 2: A minimum of 4 and not more than 5 from the first array can be considered. So we consider 4 elements from the first array. This satisfies the condition and both 11 and 13 are less than 20 and 17 respectively. So the median is max(11, 13) = 13.
Consider 4 elements from 1st array on the left of the median
Example 2: When the total number of elements is even.
Step 1: Consider 2 elements from the first array to be on the left of the median. But 14 > 5. So more elements from the first array should be considered.
Consider 2 elements from the first on the left of the median
Step 2: At least 3 and not more than 4 elements from the first array can be considered. So, we consider 3 elements. But again 12 > 8. So we need to take more from the first array.
Consider 3 elements from the first on the left of the median
Step 3: We need to consider more than 3 from the first array. So we consider all the elements from the first array and this satisfies the condition that 8 < 12. So now the median becomes the average of max(8, 10) and 12 i.e., (10 + 12)/2 = 11.
Consider all elements from the first on the left of the median
Code implementation of the above approach:
C++
// CPP code for median with case of returning// double value when even number of elements are// present in both array combinely#include <bits/stdc++.h>using std::cout;Β
int maximum(int a, int b);int minimum(int a, int b);Β
// Function to find median of two sorted arraysdouble findMedianSortedArrays(int* a, int n, int* b, int m){Β
Β Β Β Β int min_index = 0, max_index = n, i, j, median;Β
Β Β Β Β while (min_index <= max_index) {Β Β Β Β Β Β Β Β i = (min_index + max_index) / 2;Β Β Β Β Β Β Β Β j = ((n + m + 1) / 2) - i;Β
Β Β Β Β Β Β Β Β // If j is negative then the partition is notΒ Β Β Β Β Β Β Β // possible having i elements from array iΒ Β Β Β Β Β Β Β if (j < 0) {Β Β Β Β Β Β Β Β Β Β Β Β max_index = i - 1;Β Β Β Β Β Β Β Β Β Β Β Β continue;Β Β Β Β Β Β Β Β }Β
Β Β Β Β Β Β Β Β // if i = n, it means that Elements from a[] inΒ Β Β Β Β Β Β Β // the second half is an empty set. and if j = 0,Β Β Β Β Β Β Β Β // it means that Elements from b[] in the firstΒ Β Β Β Β Β Β Β // half is an empty set. so it is necessary toΒ Β Β Β Β Β Β Β // check that, because we compare elements fromΒ Β Β Β Β Β Β Β // these two groups.Β Β Β Β Β Β Β Β // Searching on rightΒ Β Β Β Β Β Β Β if (i < n && j > 0 && b[j - 1] > a[i])Β Β Β Β Β Β Β Β Β Β Β Β min_index = i + 1;Β
Β Β Β Β Β Β Β Β // if i = 0, it means that Elements from a[] inΒ Β Β Β Β Β Β Β // the first half is an empty set and if j = m,Β Β Β Β Β Β Β Β // it means that Elements from b[] in the secondΒ Β Β Β Β Β Β Β // half is an empty set. so it is necessary toΒ Β Β Β Β Β Β Β // check that, because we compare elementsΒ Β Β Β Β Β Β Β // from these two groups.Β Β Β Β Β Β Β Β // searching on leftΒ Β Β Β Β Β Β Β else if (i > 0 && j < m && b[j] < a[i - 1])Β Β Β Β Β Β Β Β Β Β Β Β max_index = i - 1;Β
Β Β Β Β Β Β Β Β // we have found the desired halves.Β Β Β Β Β Β Β Β else {Β Β Β Β Β Β Β Β Β Β Β Β // this condition happens when we don't have anyΒ Β Β Β Β Β Β Β Β Β Β Β // elements in the first half from a[] so weΒ Β Β Β Β Β Β Β Β Β Β Β // returning the last element in b[] fromΒ Β Β Β Β Β Β Β Β Β Β Β // the first half.Β Β Β Β Β Β Β Β Β Β Β Β if (i == 0)Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β median = b[j - 1];Β
Β Β Β Β Β Β Β Β Β Β Β Β // and this condition happens when we don'tΒ Β Β Β Β Β Β Β Β Β Β Β // have any elements in the first half fromΒ Β Β Β Β Β Β Β Β Β Β Β // b[] so we returning the last element inΒ Β Β Β Β Β Β Β Β Β Β Β // a[] from the first half.Β Β Β Β Β Β Β Β Β Β Β Β else if (j == 0)Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β median = a[i - 1];Β Β Β Β Β Β Β Β Β Β Β Β elseΒ Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β median = maximum(a[i - 1], b[j - 1]);Β Β Β Β Β Β Β Β Β Β Β Β break;Β Β Β Β Β Β Β Β }Β Β Β Β }Β
Β Β Β Β // calculating the median.Β Β Β Β // If number of elements is odd there isΒ Β Β Β // one middle element.Β Β Β Β if ((n + m) % 2 == 1)Β Β Β Β Β Β Β Β return (double)median;Β
Β Β Β Β // Elements from a[] in the second half is an empty set.Β Β Β Β if (i == n)Β Β Β Β Β Β Β Β return (median + b[j]) / 2.0;Β
Β Β Β Β // Elements from b[] in the second half is an empty set.Β Β Β Β if (j == m)Β Β Β Β Β Β Β Β return (median + a[i]) / 2.0;Β
Β Β Β Β return (median + minimum(a[i], b[j])) / 2.0;}Β
// Function to find maxint maximum(int a, int b) { return a > b ? a : b; }Β
// Function to find minimumint minimum(int a, int b) { return a < b ? a : b; }Β
// Driver codeint main(){Β Β Β Β int a[] = { 900 };Β Β Β Β int b[] = { 10, 13, 14 };Β Β Β Β int n = sizeof(a) / sizeof(int);Β Β Β Β int m = sizeof(b) / sizeof(int);Β
Β Β Β Β // we need to define the smaller array as theΒ Β Β Β // first parameter to make sure that theΒ Β Β Β // time complexity will be O(log(min(n,m)))Β Β Β Β if (n < m)Β Β Β Β Β Β Β Β cout << "The median is : "Β Β Β Β Β Β Β Β Β Β Β Β Β << findMedianSortedArrays(a, n, b, m);Β Β Β Β elseΒ Β Β Β Β Β Β Β cout << "The median is : "Β Β Β Β Β Β Β Β Β Β Β Β Β << findMedianSortedArrays(b, m, a, n);Β
Β Β Β Β return 0;} |
Java
// Java code for median with// case of returning double// value when even number of// elements are present in// both array combinelyimport java.io.*;Β
class GFG {Β Β Β Β static int[] a = new int[] { 900 };Β Β Β Β static int[] b = new int[] { 10, 13, 14 };Β
Β Β Β Β // Function to find maxΒ Β Β Β static int maximum(int a, int b)Β Β Β Β {Β Β Β Β Β Β Β Β return a > b ? a : b;Β Β Β Β }Β
Β Β Β Β // Function to find minimumΒ Β Β Β static int minimum(int a, int b)Β Β Β Β {Β Β Β Β Β Β Β Β return a < b ? a : b;Β Β Β Β }Β
Β Β Β Β // Function to find medianΒ Β Β Β // of two sorted arraysΒ Β Β Β static double findMedianSortedArrays(int n, int m)Β Β Β Β {Β
Β Β Β Β Β Β Β Β int min_index = 0, max_index = n, i = 0, j = 0,Β Β Β Β Β Β Β Β Β Β Β Β median = 0;Β
Β Β Β Β Β Β Β Β while (min_index <= max_index) {Β Β Β Β Β Β Β Β Β Β Β Β i = (min_index + max_index) / 2;Β Β Β Β Β Β Β Β Β Β Β Β j = ((n + m + 1) / 2) - i;Β
Β Β Β Β Β Β Β Β Β Β Β Β // if i = n, it means that ElementsΒ Β Β Β Β Β Β Β Β Β Β Β // from a[] in the second half is anΒ Β Β Β Β Β Β Β Β Β Β Β // empty set. and if j = 0, it meansΒ Β Β Β Β Β Β Β Β Β Β Β // that Elements from b[] in the firstΒ Β Β Β Β Β Β Β Β Β Β Β // half is an empty set. so it isΒ Β Β Β Β Β Β Β Β Β Β Β // necessary to check that, because weΒ Β Β Β Β Β Β Β Β Β Β Β // compare elements from these twoΒ Β Β Β Β Β Β Β Β Β Β Β // groups. Searching on rightΒ Β Β Β Β Β Β Β Β Β Β Β if (i < n && j > 0 && b[j - 1] > a[i])Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β min_index = i + 1;Β
Β Β Β Β Β Β Β Β Β Β Β Β // if i = 0, it means that ElementsΒ Β Β Β Β Β Β Β Β Β Β Β // from a[] in the first half is anΒ Β Β Β Β Β Β Β Β Β Β Β // empty set and if j = m, it meansΒ Β Β Β Β Β Β Β Β Β Β Β // that Elements from b[] in the secondΒ Β Β Β Β Β Β Β Β Β Β Β // half is an empty set. so it isΒ Β Β Β Β Β Β Β Β Β Β Β // necessary to check that, becauseΒ Β Β Β Β Β Β Β Β Β Β Β // we compare elements from these twoΒ Β Β Β Β Β Β Β Β Β Β Β // groups. searching on leftΒ Β Β Β Β Β Β Β Β Β Β Β else if (i > 0 && j < m && b[j] < a[i - 1])Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β max_index = i - 1;Β
Β Β Β Β Β Β Β Β Β Β Β Β // we have found the desired halves.Β Β Β Β Β Β Β Β Β Β Β Β else {Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β // this condition happens when weΒ Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β // don't have any elements in theΒ Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β // first half from a[] so weΒ Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β // returning the last element inΒ Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β // b[] from the first half.Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β if (i == 0)Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β median = b[j - 1];Β
Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β // and this condition happens whenΒ Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β // we don't have any elements in theΒ Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β // first half from b[] so weΒ Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β // returning the last element inΒ Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β // a[] from the first half.Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β else if (j == 0)Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β median = a[i - 1];Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β elseΒ Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β median = maximum(a[i - 1], b[j - 1]);Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β break;Β Β Β Β Β Β Β Β Β Β Β Β }Β Β Β Β Β Β Β Β }Β
Β Β Β Β Β Β Β Β // calculating the median.Β Β Β Β Β Β Β Β // If number of elements is oddΒ Β Β Β Β Β Β Β // there is one middle element.Β Β Β Β Β Β Β Β if ((n + m) % 2 == 1)Β Β Β Β Β Β Β Β Β Β Β Β return (double)median;Β
Β Β Β Β Β Β Β Β // Elements from a[] in theΒ Β Β Β Β Β Β Β // second half is an empty set.Β Β Β Β Β Β Β Β if (i == n)Β Β Β Β Β Β Β Β Β Β Β Β return (median + b[j]) / 2.0;Β
Β Β Β Β Β Β Β Β // Elements from b[] in theΒ Β Β Β Β Β Β Β // second half is an empty set.Β Β Β Β Β Β Β Β if (j == m)Β Β Β Β Β Β Β Β Β Β Β Β return (median + a[i]) / 2.0;Β
Β Β Β Β Β Β Β Β return (median + minimum(a[i], b[j])) / 2.0;Β Β Β Β }Β
Β Β Β Β // Driver codeΒ Β Β Β public static void main(String args[])Β Β Β Β {Β Β Β Β Β Β Β Β int n = a.length;Β Β Β Β Β Β Β Β int m = b.length;Β
Β Β Β Β Β Β Β Β // we need to define theΒ Β Β Β Β Β Β Β // smaller array as theΒ Β Β Β Β Β Β Β // first parameter toΒ Β Β Β Β Β Β Β // make sure that theΒ Β Β Β Β Β Β Β // time complexity willΒ Β Β Β Β Β Β Β // be O(log(min(n,m)))Β Β Β Β Β Β Β Β if (n < m)Β Β Β Β Β Β Β Β Β Β Β Β System.out.print(Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β "The median is : "Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β + findMedianSortedArrays(n, m));Β Β Β Β Β Β Β Β elseΒ Β Β Β Β Β Β Β Β Β Β Β System.out.print(Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β "The median is : "Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β + findMedianSortedArrays(m, n));Β Β Β Β }}Β
// This code is contributed by// Manish Shaw(manishshaw1) |
Python3
# Python code for median with# case of returning double# value when even number# of elements are present# in both array combinelymedian = 0i = 0j = 0Β
# def to find maxΒ
Β
def maximum(a, b):Β Β Β Β return a if a > b else bΒ
# def to find minimumΒ
Β
def minimum(a, b):Β Β Β Β return a if a < b else bΒ
# def to find median# of two sorted arraysΒ
Β
def findMedianSortedArrays(a, n, b, m):Β
Β Β Β Β Β Β # if a is emptyΒ Β Β Β Β Β if n == 0:Β Β Β Β Β Β # if b has even no. of elementsΒ Β Β Β Β Β Β Β Β Β if m % 2 == 0:Β Β Β Β Β Β Β Β Β Β Β Β return (b[m/2]+b[(m/2)+1])/2Β Β Β Β Β Β # if b has odd no. of elementsΒ Β Β Β Β Β Β Β Β Β else:Β Β Β Β Β Β Β Β Β Β Β Β return b[int(m/2)]Β
Β Β Β Β Β # if b is emptyΒ Β Β Β Β Β elif m == 0:Β Β Β Β Β Β # if a has even no. of elementsΒ Β Β Β Β Β Β Β Β Β Β if n % 2 == 0:Β Β Β Β Β Β Β Β Β Β Β Β Β Β return (a[n/2]+a[(n/2)+1])/2Β Β Β Β Β Β # if a has odd no. of elementsΒ Β Β Β Β Β else:Β Β Β Β Β Β Β Β Β Β Β return a[int(n/2)]Β
Β
global median, i, jmin_index = 0max_index = nΒ
while (min_index <= max_index):Β
Β Β Β Β Β Β Β Β i = int((min_index + max_index) / 2)Β Β Β Β Β Β Β Β j = int(((n + m + 1) / 2) - i)Β
Β Β Β Β Β Β Β Β # if i = n, it means thatΒ Β Β Β Β Β Β Β # Elements from a[] in theΒ Β Β Β Β Β Β Β # second half is an emptyΒ Β Β Β Β Β Β Β # set. and if j = 0, itΒ Β Β Β Β Β Β Β # means that Elements fromΒ Β Β Β Β Β Β Β # b[] in the first half isΒ Β Β Β Β Β Β Β # an empty set. so it isΒ Β Β Β Β Β Β Β # necessary to check that,Β Β Β Β Β Β Β Β # because we compare elementsΒ Β Β Β Β Β Β Β # from these two groups.Β Β Β Β Β Β Β Β # Searching on rightΒ Β Β Β Β Β Β Β if (i < n and j > 0 and b[j - 1] > a[i]):Β Β Β Β Β Β Β Β Β Β Β Β min_index = i + 1Β
Β Β Β Β Β Β Β Β # if i = 0, it means thatΒ Β Β Β Β Β Β Β # Elements from a[] in theΒ Β Β Β Β Β Β Β # first half is an emptyΒ Β Β Β Β Β Β Β # set and if j = m, it meansΒ Β Β Β Β Β Β Β # that Elements from b[] inΒ Β Β Β Β Β Β Β # the second half is an emptyΒ Β Β Β Β Β Β Β # set. so it is necessary toΒ Β Β Β Β Β Β Β # check that, because we compareΒ Β Β Β Β Β Β Β # elements from these two groups.Β Β Β Β Β Β Β Β # searching on leftΒ Β Β Β Β Β Β Β elif (i > 0 and j < m and b[j] < a[i - 1]):Β Β Β Β Β Β Β Β Β Β Β Β max_index = i - 1Β
Β Β Β Β Β Β Β Β # we have found theΒ Β Β Β Β Β Β Β # desired halves.Β Β Β Β Β Β Β Β else:Β
Β Β Β Β Β Β Β Β Β Β Β Β # this condition happens whenΒ Β Β Β Β Β Β Β Β Β Β Β # we don't have any elementsΒ Β Β Β Β Β Β Β Β Β Β Β # in the first half from a[]Β Β Β Β Β Β Β Β Β Β Β Β # so we returning the lastΒ Β Β Β Β Β Β Β Β Β Β Β # element in b[] from theΒ Β Β Β Β Β Β Β Β Β Β Β # first half.Β Β Β Β Β Β Β Β Β Β Β Β if (i == 0):Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β median = b[j - 1]Β
Β Β Β Β Β Β Β Β Β Β Β Β # and this condition happensΒ Β Β Β Β Β Β Β Β Β Β Β # when we don't have anyΒ Β Β Β Β Β Β Β Β Β Β Β # elements in the first halfΒ Β Β Β Β Β Β Β Β Β Β Β # from b[] so we returning theΒ Β Β Β Β Β Β Β Β Β Β Β # last element in a[] from theΒ Β Β Β Β Β Β Β Β Β Β Β # first half.Β Β Β Β Β Β Β Β Β Β Β Β elif (j == 0):Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β median = a[i - 1]Β Β Β Β Β Β Β Β Β Β Β Β else:Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β median = maximum(a[i - 1], b[j - 1])Β Β Β Β Β Β Β Β Β Β Β Β breakΒ
Β Β Β Β # calculating the median.Β Β Β Β # If number of elementsΒ Β Β Β # is odd there isΒ Β Β Β # one middle element.Β
Β Β Β Β if ((n + m) % 2 == 1):Β Β Β Β Β Β Β Β Β return medianΒ
Β Β Β Β # Elements from a[] in theΒ Β Β Β # second half is an empty set.Β Β Β Β if (i == n):Β Β Β Β Β Β Β Β return ((median + b[j]) / 2.0)Β
Β Β Β Β # Elements from b[] in theΒ Β Β Β # second half is an empty set.Β Β Β Β if (j == m):Β Β Β Β Β Β Β Β return ((median + a[i]) / 2.0)Β
Β Β Β Β return ((median + minimum(a[i], b[j])) / 2.0)Β
Β
# Driver codea = [900]b = [10, 13, 14]n = len(a)m = len(b)Β
# we need to define the# smaller array as the# first parameter to make# sure that the time complexity# will be O(log(min(n,m)))if (n < m):Β Β Β Β print("The median is : {}".format(findMedianSortedArrays(a, n, b, m)))else:Β Β Β Β echo("The median is : {}".format(findMedianSortedArrays(b, m, a, n)))Β
# This code is contributed# by Aditya Khare(adityaskhare123) |
C#
// C# code for median with case of returning// double value when even number of elements// are present in both array combinelyusing System;Β
class GFG {Β
Β Β Β Β // Function to find maxΒ Β Β Β static int maximum(int a, int b)Β Β Β Β {Β Β Β Β Β Β Β Β return a > b ? a : b;Β Β Β Β }Β
Β Β Β Β // Function to find minimumΒ Β Β Β static int minimum(int a, int b)Β Β Β Β {Β Β Β Β Β Β Β Β return a < b ? a : b;Β Β Β Β }Β
Β Β Β Β // Function to find median of two sortedΒ Β Β Β // arraysΒ Β Β Β static double findMedianSortedArrays(ref int[] a, int n,Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β ref int[] b, int m)Β Β Β Β {Β
Β Β Β Β Β Β Β Β int min_index = 0, max_index = n, i = 0, j = 0,Β Β Β Β Β Β Β Β Β Β Β Β median = 0;Β
Β Β Β Β Β Β Β Β while (min_index <= max_index) {Β Β Β Β Β Β Β Β Β Β Β Β i = (min_index + max_index) / 2;Β Β Β Β Β Β Β Β Β Β Β Β j = ((n + m + 1) / 2) - i;Β
Β Β Β Β Β Β Β Β Β Β Β Β // if i = n, it means that ElementsΒ Β Β Β Β Β Β Β Β Β Β Β // from a[] in the second half is anΒ Β Β Β Β Β Β Β Β Β Β Β // empty set. and if j = 0, it meansΒ Β Β Β Β Β Β Β Β Β Β Β // that Elements from b[] in the firstΒ Β Β Β Β Β Β Β Β Β Β Β // half is an empty set. so it isΒ Β Β Β Β Β Β Β Β Β Β Β // necessary to check that, because weΒ Β Β Β Β Β Β Β Β Β Β Β // compare elements from these twoΒ Β Β Β Β Β Β Β Β Β Β Β // groups. Searching on rightΒ Β Β Β Β Β Β Β Β Β Β Β if (i < n && j > 0 && b[j - 1] > a[i])Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β min_index = i + 1;Β
Β Β Β Β Β Β Β Β Β Β Β Β // if i = 0, it means that ElementsΒ Β Β Β Β Β Β Β Β Β Β Β // from a[] in the first half is anΒ Β Β Β Β Β Β Β Β Β Β Β // empty set and if j = m, it meansΒ Β Β Β Β Β Β Β Β Β Β Β // that Elements from b[] in the secondΒ Β Β Β Β Β Β Β Β Β Β Β // half is an empty set. so it isΒ Β Β Β Β Β Β Β Β Β Β Β // necessary to check that, becauseΒ Β Β Β Β Β Β Β Β Β Β Β // we compare elements from these twoΒ Β Β Β Β Β Β Β Β Β Β Β // groups. searching on leftΒ Β Β Β Β Β Β Β Β Β Β Β else if (i > 0 && j < m && b[j] < a[i - 1])Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β max_index = i - 1;Β
Β Β Β Β Β Β Β Β Β Β Β Β // we have found the desired halves.Β Β Β Β Β Β Β Β Β Β Β Β else {Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β // this condition happens when weΒ Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β // don't have any elements in theΒ Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β // first half from a[] so weΒ Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β // returning the last element inΒ Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β // b[] from the first half.Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β if (i == 0)Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β median = b[j - 1];Β
Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β // and this condition happens whenΒ Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β // we don't have any elements in theΒ Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β // first half from b[] so weΒ Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β // returning the last element inΒ Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β // a[] from the first half.Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β else if (j == 0)Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β median = a[i - 1];Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β elseΒ Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β median = maximum(a[i - 1], b[j - 1]);Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β break;Β Β Β Β Β Β Β Β Β Β Β Β }Β Β Β Β Β Β Β Β }Β
Β Β Β Β Β Β Β Β // calculating the median.Β Β Β Β Β Β Β Β // If number of elements is oddΒ Β Β Β Β Β Β Β // there is one middle element.Β Β Β Β Β Β Β Β if ((n + m) % 2 == 1)Β Β Β Β Β Β Β Β Β Β Β Β return (double)median;Β
Β Β Β Β Β Β Β Β // Elements from a[] in the secondΒ Β Β Β Β Β Β Β // half is an empty set.Β Β Β Β Β Β Β Β if (i == n)Β Β Β Β Β Β Β Β Β Β Β Β return (median + b[j]) / 2.0;Β
Β Β Β Β Β Β Β Β // Elements from b[] in the secondΒ Β Β Β Β Β Β Β // half is an empty set.Β Β Β Β Β Β Β Β if (j == m)Β Β Β Β Β Β Β Β Β Β Β Β return (median + a[i]) / 2.0;Β
Β Β Β Β Β Β Β Β return (median + minimum(a[i], b[j])) / 2.0;Β Β Β Β }Β
Β Β Β Β // Driver codeΒ Β Β Β static void Main()Β Β Β Β {Β Β Β Β Β Β Β Β int[] a = new int[] { 900 };Β Β Β Β Β Β Β Β int[] b = new int[] { 10, 13, 14 };Β Β Β Β Β Β Β Β int n = a.Length;Β Β Β Β Β Β Β Β int m = b.Length;Β
Β Β Β Β Β Β Β Β // we need to define the smallerΒ Β Β Β Β Β Β Β // array as the first parameter toΒ Β Β Β Β Β Β Β // make sure that the timeΒ Β Β Β Β Β Β Β // complexity will be O(log(min(n,m)))Β Β Β Β Β Β Β Β if (n < m)Β Β Β Β Β Β Β Β Β Β Β Β Console.Write("The median is : "Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β + findMedianSortedArrays(Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β ref a, n, ref b, m));Β Β Β Β Β Β Β Β elseΒ Β Β Β Β Β Β Β Β Β Β Β Console.Write("The median is : "Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β + findMedianSortedArrays(Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β ref b, m, ref a, n));Β Β Β Β }}Β
// This code is contributed by Manish Shaw// (manishshaw1) |
PHP
<?php// PHP code for median withΒ // case of returning double// value when even number // of elements are present// in both array combinely$median = 0;$i = 0; $j = 0;Β
// Function to find maxfunction maximum($a, $b) {Β Β Β Β return $a > $b ? $a : $b;}Β
// Function to find minimumfunction minimum($a, $b) {Β Β Β Β return $a < $b ? $a : $b; }Β
// Function to find median// of two sorted arraysfunction findMedianSortedArrays(&$a, $n, Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β &$b, $m){Β Β Β Β global $median, $i, $j;Β Β Β Β $min_index = 0; Β Β Β Β $max_index = $n; Β Β Β Β Β Β Β Β Β while ($min_index <= $max_index)Β Β Β Β {Β Β Β Β Β Β Β Β $i = intval(($min_index + Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β $max_index) / 2);Β Β Β Β Β Β Β Β $j = intval((($n + $m + 1) / Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β 2) - $i);Β Β Β Β Β Β Β Β Β Β Β Β Β // if i = n, it means that Β Β Β Β Β Β Β Β // Elements from a[] in theΒ Β Β Β Β Β Β Β // second half is an empty Β Β Β Β Β Β Β Β // set. and if j = 0, it Β Β Β Β Β Β Β Β // means that Elements from Β Β Β Β Β Β Β Β // b[] in the first half is Β Β Β Β Β Β Β Β // an empty set. so it is Β Β Β Β Β Β Β Β // necessary to check that, Β Β Β Β Β Β Β Β // because we compare elements Β Β Β Β Β Β Β Β // from these two groups. Β Β Β Β Β Β Β Β // Searching on rightΒ Β Β Β Β Β Β Β if ($i < $n && $j > 0 && Β Β Β Β Β Β Β Β Β Β Β Β $b[$j - 1] > $a[$i])Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β $min_index = $i + 1;Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β // if i = 0, it means that Β Β Β Β Β Β Β Β // Elements from a[] in theΒ Β Β Β Β Β Β Β // first half is an empty Β Β Β Β Β Β Β Β // set and if j = m, it meansΒ Β Β Β Β Β Β Β // that Elements from b[] in Β Β Β Β Β Β Β Β // the second half is an empty Β Β Β Β Β Β Β Β // set. so it is necessary toΒ Β Β Β Β Β Β Β // check that, because we compare Β Β Β Β Β Β Β Β // elements from these two groups.Β Β Β Β Β Β Β Β // searching on leftΒ Β Β Β Β Β Β Β else if ($i > 0 && $j < $m && Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β $b[$j] < $a[$i - 1])Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β $max_index = $i - 1;Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β // we have found theΒ Β Β Β Β Β Β Β // desired halves.Β Β Β Β Β Β Β Β elseΒ Β Β Β Β Β Β Β {Β Β Β Β Β Β Β Β Β Β Β Β // this condition happens when Β Β Β Β Β Β Β Β Β Β Β Β // we don't have any elements Β Β Β Β Β Β Β Β Β Β Β Β // in the first half from a[] Β Β Β Β Β Β Β Β Β Β Β Β // so we returning the lastΒ Β Β Β Β Β Β Β Β Β Β Β // element in b[] from the Β Β Β Β Β Β Β Β Β Β Β Β // first half.Β Β Β Β Β Β Β Β Β Β Β Β if ($i == 0) Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β $median = $b[$j - 1];Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β // and this condition happens Β Β Β Β Β Β Β Β Β Β Β Β // when we don't have any Β Β Β Β Β Β Β Β Β Β Β Β // elements in the first half Β Β Β Β Β Β Β Β Β Β Β Β // from b[] so we returning the Β Β Β Β Β Β Β Β Β Β Β Β // last element in a[] from the Β Β Β Β Β Β Β Β Β Β Β Β // first half.Β Β Β Β Β Β Β Β Β Β Β Β else if ($j == 0)Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β $median = $a[$i - 1];Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β elseΒ Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β $median = maximum($a[$i - 1], Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β $b[$j - 1]); Β Β Β Β Β Β Β Β Β Β Β Β break;Β Β Β Β Β Β Β Β }Β Β Β Β }Β Β Β Β Β Β Β Β Β // calculating the median.Β Β Β Β // If number of elements Β Β Β Β // is odd there is Β Β Β Β // one middle element.Β
Β Β Β Β if (($n + $m) % 2 == 1)Β Β Β Β Β Β Β Β return $median;Β
Β Β Β Β // Elements from a[] in the Β Β Β Β // second half is an empty set. Β Β Β Β if ($i == $n)Β Β Β Β Β Β Β Β return (($median + Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β $b[$j]) / 2.0);Β
Β Β Β Β // Elements from b[] in the Β Β Β Β // second half is an empty set.Β Β Β Β if ($j == $m)Β Β Β Β Β Β Β Β return (($median + Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β $a[$i]) / 2.0);Β Β Β Β Β Β Β Β Β return (($median + Β Β Β Β Β Β Β Β Β Β Β Β Β minimum($a[$i], Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β $b[$j])) / 2.0);}Β
// Driver code$a = array(900);$b = array(10, 13, 14);$n = count($a);$m = count($b);Β
// we need to define the // smaller array as the // first parameter to make // sure that the time complexity// will be O(log(min(n,m)))if ($n < $m)Β Β Β Β echo ("The median is : " . Β Β Β Β Β Β Β Β Β Β Β findMedianSortedArrays($a, $n, Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β $b, $m));elseΒ Β Β Β echo ("The median is : " . Β Β Β Β Β Β Β Β Β Β Β findMedianSortedArrays($b, $m, Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β $a, $n));Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β // This code is contributed // by Manish Shaw(manishshaw1)?> |
Javascript
<script>// Javascript code for median with// case of returning double// value when even number of// elements are present in// both array combinelyΒ Β Β Β Β Β Β Β Β let a=[900];Β Β Β Β let b=[10, 13, 14];Β Β Β Β Β Β Β Β Β Β // Function to find maxΒ Β Β Β function maximum(a,b)Β Β Β Β {Β Β Β Β Β Β Β Β return a > b ? a : b;Β Β Β Β }Β Β Β Β Β Β Β Β Β // Function to find minimumΒ Β Β Β function minimum(a,b)Β Β Β Β {Β Β Β Β Β Β Β Β return a < b ? a : b;Β Β Β Β }Β Β Β Β Β Β Β Β Β // Function to find medianΒ Β Β Β // of two sorted arraysΒ Β Β Β function findMedianSortedArrays(n,m)Β Β Β Β {Β Β Β Β Β Β Β Β let min_index = 0,Β Β Β Β Β Β Β Β Β Β Β Β max_index = n, i = 0,Β Β Β Β Β Β Β Β Β Β Β Β j = 0, median = 0;Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β while (min_index <= max_index)Β Β Β Β Β Β Β Β {Β Β Β Β Β Β Β Β Β Β Β Β i = Math.floor((min_index + max_index) / 2);Β Β Β Β Β Β Β Β Β Β Β Β j = Math.floor((n + m + 1) / 2) - i;Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β // if i = n, it means that ElementsΒ Β Β Β Β Β Β Β Β Β Β Β // from a[] in the second half is anΒ Β Β Β Β Β Β Β Β Β Β Β // empty set. and if j = 0, it meansΒ Β Β Β Β Β Β Β Β Β Β Β // that Elements from b[] in the firstΒ Β Β Β Β Β Β Β Β Β Β Β // half is an empty set. so it isΒ Β Β Β Β Β Β Β Β Β Β Β // necessary to check that, because weΒ Β Β Β Β Β Β Β Β Β Β Β // compare elements from these twoΒ Β Β Β Β Β Β Β Β Β Β Β // groups. Searching on rightΒ Β Β Β Β Β Β Β Β Β Β Β if (i < n && j > 0 && b[j - 1] > a[i])Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β min_index = i + 1;Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β // if i = 0, it means that ElementsΒ Β Β Β Β Β Β Β Β Β Β Β // from a[] in the first half is anΒ Β Β Β Β Β Β Β Β Β Β Β // empty set and if j = m, it meansΒ Β Β Β Β Β Β Β Β Β Β Β // that Elements from b[] in the secondΒ Β Β Β Β Β Β Β Β Β Β Β // half is an empty set. so it isΒ Β Β Β Β Β Β Β Β Β Β Β // necessary to check that, becauseΒ Β Β Β Β Β Β Β Β Β Β Β // we compare elements from these twoΒ Β Β Β Β Β Β Β Β Β Β Β // groups. searching on leftΒ Β Β Β Β Β Β Β Β Β Β Β else if (i > 0 && j < m && b[j] < a[i - 1])Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β max_index = i - 1;Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β // we have found the desired halves.Β Β Β Β Β Β Β Β Β Β Β Β elseΒ Β Β Β Β Β Β Β Β Β Β Β {Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β // this condition happens when weΒ Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β // don't have any elements in theΒ Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β // first half from a[] so weΒ Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β // returning the last element inΒ Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β // b[] from the first half.Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β if (i == 0)Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β median = b[j - 1];Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β // and this condition happens whenΒ Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β // we don't have any elements in theΒ Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β // first half from b[] so weΒ Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β // returning the last element inΒ Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β // a[] from the first half.Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β else if (j == 0)Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β median = a[i - 1];Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β elseΒ Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β median = maximum(a[i - 1],Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β b[j - 1]);Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β break;Β Β Β Β Β Β Β Β Β Β Β Β }Β Β Β Β Β Β Β Β }Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β // calculating the median.Β Β Β Β Β Β Β Β // If number of elements is oddΒ Β Β Β Β Β Β Β // there is one middle element.Β Β Β Β Β Β Β Β if ((n + m) % 2 == 1)Β Β Β Β Β Β Β Β Β Β Β Β return median;Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β // Elements from a[] in theΒ Β Β Β Β Β Β Β // second half is an empty set.Β Β Β Β Β Β Β Β if (i == n)Β Β Β Β Β Β Β Β Β Β Β Β return (median + b[j]) / 2.0;Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β // Elements from b[] in theΒ Β Β Β Β Β Β Β // second half is an empty set.Β Β Β Β Β Β Β Β if (j == m)Β Β Β Β Β Β Β Β Β Β Β Β return (median + a[i]) / 2.0;Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β return (median + minimum(a[i],Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β b[j])) / 2.0;Β Β Β Β }Β Β Β Β Β Β Β Β Β // Driver codeΒ Β Β Β let n = a.length;Β Β Β Β let m = b.length;Β Β Β Β Β Β Β Β Β // we need to define theΒ Β Β Β // smaller array as theΒ Β Β Β // first parameter toΒ Β Β Β // make sure that theΒ Β Β Β // time complexity willΒ Β Β Β // be O(log(min(n,m)))Β Β Β Β if (n < m)Β Β Β Β Β Β Β Β document.write("The median is : " +Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β findMedianSortedArrays(n, m));Β Β Β Β elseΒ Β Β Β Β Β Β Β document.write("The median is : " +Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β findMedianSortedArrays(m, n));Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β // This code is contributed by unknown2108</script> |
The median is : 13.5
Time Complexity: O(log(min(n, m))
Auxiliary Space: O(1), the space complexity of this algorithm is O(1), as we are not using any extra space.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



