Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions median of two sorted arrays leetcode hard
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
double findMedianSortedArrays(vector<int>& vec1, vector<int>& vec2) {

if(vec2.size() < vec1.size()) {
return findMedianSortedArrays(vec2 , vec1);
}
int n1 = vec1.size();
int n2 = vec2.size();
int low = 0,high = n1;

while(low <= high)
{

int cut1 = (low + high)>>1;
int cut2 = (n1 + n2 + 1 )/2 - cut1;

int l1 = cut1 == 0 ? INT_MIN : vec1[cut1-1];
int l2 = cut2 == 0 ? INT_MIN : vec2[cut2-1];

int r1 = cut1 == n1 ? INT_MAX : vec1[cut1];
int r2 = cut2 == n2 ? INT_MAX : vec2[cut2];


if(l1 <= r2 && l2 <= r1)
{
if((n1+n2)%2 == 0)
{
return (max(l1,l2) + min(r1,r2))/2.0;
}
else
{
return max(l1,l2);
}
}
else if(l1 > r2)
{
high = cut1 - 1;
}
else
{
low = cut1 + 1;
}
}
return 0.0;
}