-
Notifications
You must be signed in to change notification settings - Fork 77
/
MedianOfTwoSortedArrays.cpp
63 lines (50 loc) · 1.15 KB
/
MedianOfTwoSortedArrays.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Problem Statement - Given two sorted arrays nums1 and nums2 of size m and n respectively, return the median of the two sorted arrays.
// Explanation
// Input: nums1 = [1,3], nums2 = [2]
// Output: 2.00000
// Explanation: merged array = [1,2,3] and median is 2
// C++ program to find the median of two sorted arrays
#include <bits/stdc++.h>
using namespace std;
int Solution(int arr[], int n)
{
// If length of array is even
if (n % 2 == 0)
{
int z = n / 2;
int e = arr[z];
int q = arr[z - 1];
int ans = (e + q) / 2;
return ans;
}
// If length if array is odd
else
{
int z = round(n / 2);
return arr[z];
}
}
// Driver Code
int main() {
// TODO Auto-generated method stub
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;
// Merge two array into one array
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 the merged array
sort(arr3,arr3+l);
// calling the method
cout<<"Median = " << Solution(arr3, l);
}