forked from taruneshrai/Array
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergetwosortedArray.cpp
More file actions
38 lines (33 loc) · 996 Bytes
/
MergetwosortedArray.cpp
File metadata and controls
38 lines (33 loc) · 996 Bytes
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
class Solution {
public:
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
int n = nums1.size();
int m = nums2.size();
if(n>m)
return findMedianSortedArrays(nums2,nums1);
int lo = 0;
int high = n;
while(lo<=high)
{
int mid = lo+(high-lo)/2;
int mid2 = (n+m)/2 - mid;
int l1 = mid==0?INT_MIN:nums1[mid-1];
int r1 = mid==n?INT_MAX:nums1[mid];
int l2 = mid2==0?INT_MIN:nums2[mid2-1];
int r2 = mid2==n?INT_MAX:nums2[mid2];
if(l1>r2)
{
high = mid-1;
}
else if(l2>r1)
{
lo = mid+1;
}
else if(l1<=r2 && l2<=r1)
{
return (n+m)%2 == 0? (double((max(l1,l2)+min(r1,r2))/2.0)) : min(r1,r2);
}
}
return 0.0;
}
};