forked from wahyuhjr/OpenEmailGenerator
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMedianOfTwoSortedArrays.cpp
62 lines (41 loc) · 1.01 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
// Median of two sorted arrays problem
#include<bits/stdc++.h>
using namespace std;
class Solution{
public:
double MedianOfArrays(vector<int>& array1, vector<int>& array2)
{
// Your code goes here
vector <int> v;
for(int i=0;i<array1.size();i++){
v.push_back(array1[i]);
}
for(int i=0;i<array2.size();i++){
v.push_back(array2[i]);
}
sort(v.begin(),v.end());
if(v.size()%2!=0){
return v[(v.size()-1)/2];
}
return (double)(v[(v.size()-1)/2]+v[v.size()/2])/2;
}
};
int main(){
int T;
cin>>T;
while(T--){
int m,n;
cin>>m>>n;
vector<int> array1(m);
for(int i=0;i<m;i++){
cin>>array1[i];
}
vector<int> array2(n);
for(int i=0;i<n;i++){
cin>>array2[i];
}
Solution ob;
cout<<ob.MedianOfArrays(array1, array2)<<" \n";
}
return 0;
}