Skip to content
Merged
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
78 changes: 78 additions & 0 deletions LeetCode/Hard/4. Median of Two Sorted Arrays.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Given two sorted arrays nums1 and nums2 of size m and n respectively, return the median of the two sorted arrays.

// The overall run time complexity should be O(log (m+n)).

// Example 1:

// Input: nums1 = [1,3], nums2 = [2]
// Output: 2.00000
// Explanation: merged array = [1,2,3] and median is 2.
// Example 2:

// Input: nums1 = [1,2], nums2 = [3,4]
// Output: 2.50000
// Explanation: merged array = [1,2,3,4] and median is (2 + 3) / 2 = 2.5.




//Approach 1

class Solution {
public:
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
for(int i=0;i<nums2.size();i++){
nums1.push_back(nums2[i]);
}
sort(nums1.begin(),nums1.end());
int n=nums1.size();
double res;
if(n&1)
res=nums1[n/2];
else if(!(n&1))
res=(double)(nums1[n/2]+nums1[(n/2)-1])/2;

return res;

}
};


// Approach 2

class Solution {
public:
double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
if(nums2.size() < nums1.size()) return findMedianSortedArrays(nums2, nums1);
int n1 = nums1.size();
int n2 = nums2.size();
int low = 0, high = n1;

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


int left1 = cut1 == 0 ? INT_MIN : nums1[cut1-1];
int left2 = cut2 == 0 ? INT_MIN : nums2[cut2-1];

int right1 = cut1 == n1 ? INT_MAX : nums1[cut1];
int right2 = cut2 == n2 ? INT_MAX : nums2[cut2];


if(left1 <= right2 && left2 <= right1) {
if( (n1 + n2) % 2 == 0 )
return (max(left1, left2) + min(right1, right2)) / 2.0;
else
return max(left1, left2);
}
else if(left1 > right2) {
high = cut1 - 1;
}
else {
low = cut1 + 1;
}
}
return 0.0;
}
};