-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7 from jainsras/jainsras
added Median_of_two_sorted_arrays.cpp
- Loading branch information
Showing
1 changed file
with
42 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} | ||
|
||
}; |