This is an extension of the median of two sorted arrays of equal size problem. Here we handle arrays of unequal size also.
Examples:
Input: a[] = {1, 12, 15, 26, 38}
b[] = {2, 13, 24}
Output: 14
Explanation:
After merging arrays the result is
1, 2, 12, 13, 15, 24, 26, 38
median = (13 + 15)/2 = 14.
Input: a[] = {1, 3, 5, 7}
b[] = {2, 4, 6, 8}
Output: 5
Explanation :
After merging arrays the result is
1, 2, 3, 4, 5, 6, 7, 8, 9
median = 5
Approach:
- The approach discussed in this post is similar to method 1 of equal size median. Use merge procedure of merge sort.
- Keep a variable to track of count while comparing elements of two arrays.
- Perform merge of two sorted arrays. Increase the value of count as a new element is inserted.
- The most efficient way of merging two arrays is to compare the top of both arrays and pop the smaller value and increase the count.
- Continue comparing the top/first elements of both arrays until there are no elements left in any arrays
- Now to find the median there are two cases to be handled.
- If the sum of length of array sizes is even then store the elements when the count is of value ((n1 + n2)/2)-1 and (n1 + n2)/2 in the merged array add the elements and divide by 2 return the value as median.
- If the value(sum of sizes) is odd then return the (n1 + n2)/2 th element(when value of count is (n1 + n2)/2) as median.
C++
#include <bits/stdc++.h>
using namespace std;
float findmedian( int a[], int n1, int b[], int n2)
{
int i = 0;
int j = 0;
int k;
int m1 = -1, m2 = -1;
for (k = 0; k <= (n1 + n2) / 2; k++) {
if (i < n1 && j < n2) {
if (a[i] < b[j]) {
m2 = m1;
m1 = a[i];
i++;
}
else {
m2 = m1;
m1 = b[j];
j++;
}
}
else if (i == n1) {
m2 = m1;
m1 = b[j];
j++;
}
else if (j == n2) {
m2 = m1;
m1 = a[i];
i++;
}
}
if ((n1 + n2) % 2 == 0)
return (m1 + m2) * 1.0 / 2;
return m1;
}
int main()
{
int a[] = { 1, 12, 15, 26, 38 };
int b[] = { 2, 13, 24 };
int n1 = sizeof (a) / sizeof (a[0]);
int n2 = sizeof (b) / sizeof (b[0]);
printf ( "%f" , findmedian(a, n1, b, n2));
return 0;
}
|
Java
import java.io.*;
class GFG {
static float findmedian( int a[], int n1, int b[], int n2)
{
int i = 0 ;
int j = 0 ;
int k;
int m1 = - 1 , m2 = - 1 ;
for (k = 0 ; k <= (n1 + n2) / 2 ; k++) {
if (i < n1 && j < n2) {
if (a[i] < b[j]) {
m2 = m1;
m1 = a[i];
i++;
}
else {
m2 = m1;
m1 = b[j];
j++;
}
}
else if (i == n1) {
m2 = m1;
m1 = b[j];
j++;
}
else if (j == n2) {
m2 = m1;
m1 = a[i];
i++;
}
}
if ((n1 + n2) % 2 == 0 ) {
return (m1 + m2) * ( float ) 1.0 / 2 ;
}
return m1;
}
public static void main(String[] args)
{
int a[] = { 1 , 12 , 15 , 26 , 38 };
int b[] = { 2 , 13 , 24 };
int n1 = a.length;
int n2 = b.length;
System.out.println(findmedian(a, n1, b, n2));
}
}
|
Python3
def findmedian(a, n1, b, n2):
i = 0
j = 0
m1 = - 1
m2 = - 1
for k in range (((n1 + n2) / / 2 ) + 1 ) :
if (i < n1 and j < n2) :
if (a[i] < b[j]) :
m2 = m1
m1 = a[i]
i + = 1
else :
m2 = m1
m1 = b[j]
j + = 1
elif (i = = n1) :
m2 = m1
m1 = b[j]
j + = 1
elif (j = = n2) :
m2 = m1
m1 = a[i]
i + = 1
if ((n1 + n2) % 2 = = 0 ):
return (m1 + m2) * 1.0 / 2
return m1
if __name__ = = "__main__" :
a = [ 1 , 12 , 15 , 26 , 38 ]
b = [ 2 , 13 , 24 ]
n1 = len (a)
n2 = len (b)
print (findmedian(a, n1, b, n2))
|
C#
using System;
class GFG {
static float findmedian( int [] a, int n1,
int [] b, int n2)
{
int i = 0;
int j = 0;
int k;
int m1 = -1, m2 = -1;
for (k = 0; k <= (n1 + n2) / 2; k++) {
if (i < n1 && j < n2) {
if (a[i] < b[j]) {
m2 = m1;
m1 = a[i];
i++;
}
else {
m2 = m1;
m1 = b[j];
j++;
}
}
else if (i == n1) {
m2 = m1;
m1 = b[j];
j++;
}
else if (j == n2) {
m2 = m1;
m1 = a[i];
i++;
}
}
if ((n1 + n2) % 2 == 0) {
return (m1 + m2) * ( float )1.0 / 2;
}
return m1;
}
public static void Main()
{
int [] a = { 1, 12, 15, 26, 38 };
int [] b = { 2, 13, 24 };
int n1 = a.Length;
int n2 = b.Length;
Console.WriteLine(findmedian(a, n1, b, n2));
}
}
|
PHP
<?php
function findmedian( $a , $n1 , $b , $n2 )
{
$i = 0;
$j = 0;
$k ;
$m1 = -1; $m2 = -1;
for ( $k = 0;
$k <= ( $n1 + $n2 ) / 2; $k ++)
{
if ( $i < $n1 and $j < $n2 )
{
if ( $a [ $i ] < $b [ $j ])
{
$m2 = $m1 ;
$m1 = $a [ $i ];
$i ++;
}
else
{
$m2 = $m1 ;
$m1 = $b [ $j ];
$j ++;
}
}
else if (i == n1)
{
$m2 = $m1 ;
$m1 = $b [j];
$j ++;
}
else if ( $j == $n2 )
{
$m2 = $m1 ;
$m1 = $a [ $i ];
$i ++;
}
}
if (( $n1 + $n2 ) % 2 == 0)
return ( $m1 + $m2 ) * 1.0 / 2;
return m1;
}
$a = array ( 1, 12, 15, 26, 38 );
$b = array ( 2, 13, 24 );
$n1 = count ( $a );
$n2 = count ( $b );
echo (findmedian( $a , $n1 , $b , $n2 ));
?>
|
Javascript
<script>
function findmedian(a, n1, b, n2)
{
let i = 0;
let j = 0;
let k;
let m1 = -1, m2 = -1;
for (k = 0; k <= (n1 + n2) / 2; k++)
{
if (i < n1 && j < n2)
{
if (a[i] < b[j])
{
m2 = m1;
m1 = a[i];
i++;
}
else
{
m2 = m1;
m1 = b[j];
j++;
}
}
else if (i == n1)
{
m2 = m1;
m1 = b[j];
j++;
}
else if (j == n2)
{
m2 = m1;
m1 = a[i];
i++;
}
}
if ((n1 + n2) % 2 == 0)
{
return (m1 + m2) * 1.0 / 2;
}
return m1;
}
let a = [ 1, 12, 15, 26, 38 ];
let b = [ 2, 13, 24 ];
let n1 = a.length;
let n2 = b.length;
document.write(findmedian(a, n1, b, n2));
</script>
|
Complexity Analysis:
- Time Complexity: O(n), Both the lists need to be traversed so the time complexity is O(n).
- Auxiliary Space: O(1), no extra space is required.
More Efficient (Log time complexity methods)
- Median of two sorted arrays of different sizes.
- Median of two sorted arrays of different sizes in min(Log (n1, n2))
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
14 May, 2021
Like Article
Save Article