Skip to content

Commit

Permalink
Create Median_of_two_sorted_arrays_of_different_sizes.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
akanksha0812 authored Oct 13, 2022
1 parent 8f6151d commit 8e49f00
Showing 1 changed file with 48 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
#include <bits/stdc++.h>
using namespace std;

int Solution(int arr[], int n)
{

if (n % 2 == 0)
{
int z = n / 2;
int e = arr[z];
int q = arr[z - 1];
int ans = (e + q) / 2;
return ans;
}

else
{
int z = round(n / 2);
return arr[z];
}
}

int main() {

int arr1[] = { -5, 3, 6, 12, 15 };
int arr2[] = { -12, -10, -6, -3, 4, 10 };

int i = sizeof(arr1) / sizeof(arr1[0]);
int j = sizeof(arr2) / sizeof(arr2[0]);

int arr3[i+j];
int l = i+j;

for(int k=0;k<i;k++)
{
arr3[k]=arr1[k];
}

int a=0;
for(int k=i;k<l;k++)
{
arr3[k]=arr2[a++];
}

sort(arr3,arr3+l);

cout<<"Median = " << Solution(arr3, l);
}

0 comments on commit 8e49f00

Please sign in to comment.