forked from ZoranPandovski/al-go-rithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create Median_of_two_sorted_arrays_of_different_sizes.cpp
- Loading branch information
1 parent
8f6151d
commit 8e49f00
Showing
1 changed file
with
48 additions
and
0 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
data_structures/Arrays/CPP/Median_of_two_sorted_arrays_of_different_sizes.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |