-
Notifications
You must be signed in to change notification settings - Fork 0
/
Week1_Q2.cpp
37 lines (37 loc) · 953 Bytes
/
Week1_Q2.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
class Solution {
public:
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
int n = nums1.size() + nums2.size();
int merged[n];
int mid = n/2;
double median;
int i = 0, j = 0, k = 0;
int count = 0, curr;
while(count<n){
if(i<nums1.size() and j<nums2.size()){
if(nums1[i]<=nums2[j]){
curr = nums1[i++];
}
else{
curr = nums2[j++];
}
}
else if(i<nums1.size()){
curr = nums1[i++];
}
else{
curr = nums2[j++];
}
merged[k++] = curr;
count++;
}
if(n%2==0){
median = merged[mid] + merged[mid-1];
median /= 2;
}
else{
median = merged[mid];
}
return median;
}
};