From 135574a2779da2f304157f8e131571c27b175875 Mon Sep 17 00:00:00 2001 From: Kalpitadhao77 <82868584+Kalpitadhao77@users.noreply.github.com> Date: Sun, 6 Jun 2021 23:01:37 +0530 Subject: [PATCH] Add files via upload --- c++/Median of 2 sorted arrays.cpp | 66 +++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 c++/Median of 2 sorted arrays.cpp diff --git a/c++/Median of 2 sorted arrays.cpp b/c++/Median of 2 sorted arrays.cpp new file mode 100644 index 0000000..5870c80 --- /dev/null +++ b/c++/Median of 2 sorted arrays.cpp @@ -0,0 +1,66 @@ +#include +using namespace std; + +int Med(int ar1[], int ar2[], int n, int m) +{ +int i = 0; +int j = 0; +int c; +int m1 = -1, m2 = -1; + +if((m + n) % 2 == 1) { +for (c = 0; c <= (n + m)/2; c++) { +if(i != n && j != m){ +m1 = (ar1[i] > ar2[j]) ? ar2[j++] : ar1[i++]; +} +else if(i < n){ +m1 = ar1[i++]; +} +else{ +m1 = ar1[j++]; +} +} +return m1; +} + +else { +for (c = 0; c <= (n + m)/2; c++) { +m2 = m1; +if(i != n && j != m){ +m1 = (ar1[i] > ar2[j]) ? ar2[j++] : ar1[i++]; +} +else if(i < n){ +m1 = ar1[i++]; +} +else{ +m1 = ar1[j++]; +} +} +return (m1 + m2)/2; +} +} + +int main() +{ +int m,n,i; +cout << "\nEnter the size of array 1 : "; +cin >> m; +cout << "\nEnter the size of array 2 : "; +cin >> n; +int ar1[m]; +int ar2[n]; + +cout << "\nInput the array 1 elements : "; +for(i = 0; i < m; i++) +cin >> ar1[i]; + +cout << "\nInput the array 2 elements : "; +for(i = 0; i < n; i++) +cin >> ar2[i]; + +cout << "\nMedian of two arrays : "; +cout << Med(ar1, ar2, m, n); +cout << endl; +return 0; +} +