Skip to content

Commit

Permalink
Merge pull request #7 from jainsras/jainsras
Browse files Browse the repository at this point in the history
added Median_of_two_sorted_arrays.cpp
  • Loading branch information
Ashish-kumar7 authored Oct 9, 2020
2 parents 2646a81 + 7338fff commit 61f7206
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions Median_of_two_sorted_arrays.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
class Solution {
public:
double find(vector<int>& nums1, vector<int>& nums2)
{
int n = nums1.size(),m = nums2.size();

int l = 0,h = n;
while(l<=h)
{
int part1= (l+h)/2;
int part2=(n+m+1)/2 - part1;

int l1=(part1==0)? INT_MIN: nums1[part1-1];
int r1= (part1==n)? INT_MAX: nums1[part1];
int l2=(part2==0)? INT_MIN: nums2[part2-1];
int r2= (part2==m)? INT_MAX: nums2[part2];

if(l1<=r2 && l2<=r1)
{
if((n+m)%2)
return (double)max(l1,l2);
else
{
return (double)(max(l1,l2)+ min(r1,r2))/2.0;
}
}

else if(l1>r2)
h=part1-1;
else
l=part1+1;
}
return 0;
}
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
int n=nums1.size(),m=nums2.size();
if(n>m)return find(nums2,nums1);
else return find(nums1,nums2);

}

};

0 comments on commit 61f7206

Please sign in to comment.