Find median of two sorted arrays of different sizes

 Problem statement:- Program to Find the median of two sorted arrays of different sizes.

Example:-

              Input : Given 

                                size=6

                                 arr[]=[4,6,8,10,12,14]

                                 size2=4

                                 arr2[]=[5,7,9,11]

                                 /*merge_arr[]=arr[]+arr2[]

                                   merge_arr[]=[4,5,6,7,8,9,10,11,12,14]*/

             Output:  median=8.50 

                       /*merge_arr[]=[4,5,6,7,8,9,10,11,12,14]

                       m_size=6+4=10//merge array size

                        median=(arr[10/2]+(arr[(10/2)-1]))/2.0

                                    =(arr[5]+arr[4])/2

                                    =(9+8)/2=17/2=8.50*/      

Data requirement:-

   Input Data:- size, arr,size2,arr2

  Output Data:-median

  Additional Data:- i,j,k,m_size,merge_arr

Program in C

Here is the source code of the C Program to Find the median of two sorted arrays of different sizes.

Code:

#include<stdio.h>
void Find_median(int arr[],int arr2[],int size,int size2)
{
    int m_size=size+size2;
    int merge_arr[m_size];
    int i=0,k=0,j=0;
    while(k<m_size)
    {
    if(i<size)
    {
        merge_arr[k]=arr[i];
        i++;
        k++;
    }
    if(j<size2)
    {
        merge_arr[k]=arr2[j];
        j++;
        k++;
    }}

    if(m_size%2==1)
    {
    double median=merge_arr[m_size/2];
   printf("Median= %0.2lf",median);
}
   else
   {
       double median=(merge_arr[m_size/2]+(merge_arr[(m_size/2)-1]))/2.0;
printf("Median= %0.2lf\n",median);
    }
    }
int main()
{
     printf("Enter the size of the 1st array:");
    int size,i;
    scanf("%d",&size);
    int arr[size];

    printf("Enter the size of the 2nd array:");
    int size2;
    scanf("%d",&size2);
    int arr2[size2];

    printf("Enter the Element of the 1st array:\n");
     for(i=0;i<size;i++)
       {
        scanf("%d",&arr[i]);
        }

    printf("Enter the Element of the 2nd array:\n");
     for(i=0;i<size2;i++)
       {
        scanf("%d",&arr2[i]);
       }
    Find_median(arr,arr2,size,size2);
}

Input/Output:
Enter the size of the 1st array:6
Enter the size of the 2nd array:4
Enter the Element of the 1st array:
4 6 8 10 12 14
Enter the Element of the 2nd array:
5 7 9 11
Median= 8.50

Program in C++

Here is the source code of the C++ Program to Find the median of two sorted arrays of different sizes.

Code:

#include<iostream>
using namespace std;
void Find_median(int arr[],int arr2[],int size,int size2)
{
    int m_size=size+size2;
    int merge_arr[m_size];
    int i=0,k=0,j=0;
    while(k<m_size)
    {
    if(i<size)
    {
        merge_arr[k]=arr[i];
        i++;
        k++;
    }
    if(j<size2)
    {
        merge_arr[k]=arr2[j];
        j++;
        k++;
    }}

    if(m_size%2==1)
    {
    double median=merge_arr[m_size/2];
   cout<<"Median= "<<median;
}
   else
   {
    double median=(merge_arr[m_size/2]+(merge_arr[(m_size/2)-1]))/2.0;
cout<<"Median= "<<median;
    }
    }
int main()
{
   cout<<"Enter the size of the 1st array:";
    int size,i;
    cin>>size;
    int arr[size];

    cout<<"Enter the size of the 2nd array:";
    int size2;
    cin>>size2;
    int arr2[size2];

    cout<<"Enter the Element of the 1st array:\n";
     for(i=0;i<size;i++)
       {
        cin>>arr[i];
        }

    cout<<"Enter the Element of the 2nd array:\n";
     for(i=0;i<size2;i++)
       {
        cin>>arr2[i];
        }
    Find_median(arr,arr2,size,size2);
}

Input/Output:
Enter the size of the 1st array:4
Enter the size of the 2nd array:3
Enter the Element of the 1st array:
7 10 14 18
Enter the Element of the 2nd array:
9 12 15
Median= 12

Program in Java

Here is the source code of the Java Program to Find the median of two sorted arrays of different sizes.

Code:

import java.util.Scanner;
public class UnsortedArrayMedian {

static void Find_median(int arr[],int arr2[],int size,int size2)
{
    int m_size=size+size2;
    int merge_arr[ ]=new int[m_size];
    int i=0,k=0,j=0;
    while(k<m_size)
    {
    if(i<size)
    {
        merge_arr[k]=arr[i];
        i++;
        k++;
    }
    if(j<size2)
    {
        merge_arr[k]=arr2[j];
        j++;
        k++;
    }}

    if(m_size%2==1)
    {
    double median=merge_arr[m_size/2];
    System.out.println("Median= "+median);
}
   else
   {
       double median=(merge_arr[m_size/2]+(merge_arr[(m_size/2)-1]))/2.0;
       System.out.println("Median= "+median);
    }
    }
public static void main(String[] args) {
  Scanner sc=new Scanner(System.in);
  System.out.println("Enter the size of the array:");
     int size,j=0;
     size=sc.nextInt();
     System.out.println("Enter the size of the 2nd array:");
     int size2=sc.nextInt();

     int arr[ ]=new int[size];
     int arr2[ ]=new int[size2];
  System.out.println("Enter the Element of the array:");
      for(j=0;j<size;j++)
     {
         arr[j]=sc.nextInt();
     }
      
      System.out.println("Enter the Element of the array:");
      for(j=0;j<size2;j++)
     {
         arr2[j]=sc.nextInt();
     }
     
      Find_median(arr,arr2,size,size2);
     sc.close();
}
}

Input/Output:
Enter the size of the 1st array:
3
Enter the size of the 2nd array:
4
Enter the Element of the 1st array:10 13 19
Enter the Element of the 2nd array:
12 14 20 22
Median= 14.0

Program in Python

Here is the source code of the Python Program to Find the median of two sorted arrays of different sizes.

Code:

def Find_median(arr,arr2,size,size2):
    m_size = size + size2
    merge_arr = [0]*m_size
    i=0
    k=0
    j=0
    while k<m_size:
       if i<size:
           merge_arr[k] = arr[i]
           i+=1
           k+=1

       if j<size2:
           merge_arr[k] = arr2[j]
           j+=1
           k+=1
    if size % 2 == 1:
        median = merge_arr[size // 2]
        print("\nMedian= ", median)
    else:
        median = (merge_arr[m_size // 2] + (merge_arr[(m_size // 2) - 1])) / 2.0
        print("\nMedian= ", median)

arr=[]
arr2=[]
size = int(input("Enter the size of the 1st array: "))
size2 = int(input("Enter the size of the 2nd array: "))

print("Enter the Element of the 1st array:")
for i in range(0,size):
    num = int(input())
    arr.append(num)

print("Enter the Element of the 2nd array:")
for i in range(0,size2):
    num2 = int(input())
    arr2.append(num2)

Find_median(arr,arr2,size,size2)

Input/Output:
Enter the size of the 1st array: 3
Enter the size of the 2nd array: 2
Enter the Element of the 1st array:
4
8
10
Enter the Element of the 2nd array:
7
9

Median=  7


Post a Comment

0 Comments