Brute
double median(vector<int>& nums1, vector<int>& nums2) {
int i = 0;
int j = 0;
int n1 = nums1.size();
int n2 = nums2.size();
vector<int> nums3;
while (i < n1 && j < n2) {
if (nums1[i] < nums2[j]) {
nums3.push_back(nums1[i]);
i++;
} else {
nums3.push_back(nums2[j]);
j++;
}
}
while (i < n1) {
nums3.push_back(nums1[i]);
i++;
}
while (j < n2) {
nums3.push_back(nums2[j]);
j++;
}
int n3 = n1 + n2;
if (n3 % 2 == 1) {
return (double)nums3[n3 / 2];
} else {
return ((double)nums3[n3 / 2] + (double)nums3[(n3 / 2) - 1]) / 2.0;
}
}
Better
#include <bits/stdc++.h>
double median(vector<int>& a, vector<int>& b) {
//size of two given arrays:
int n1 = a.size(), n2 = b.size();
int n = n1 + n2; //total size
//required indices:
int ind2 = n / 2;
int ind1 = ind2 - 1;
int cnt = 0;
int ind1el = -1, ind2el = -1;
//apply the merge step:
int i = 0, j = 0;
while (i < n1 && j < n2) {
if (a[i] < b[j]) {
if (cnt == ind1) ind1el = a[i];
if (cnt == ind2) ind2el = a[i];
cnt++;
i++;
}
else {
if (cnt == ind1) ind1el = b[j];
if (cnt == ind2) ind2el = b[j];
cnt++;
j++;
}
}
//copy the left-out elements:
while (i < n1) {
if (cnt == ind1) ind1el = a[i];
if (cnt == ind2) ind2el = a[i];
cnt++;
i++;
}
while (j < n2) {
if (cnt == ind1) ind1el = b[j];
if (cnt == ind2) ind2el = b[j];
cnt++;
j++;
}
//Find the median:
if (n % 2 == 1) {
return (double)ind2el;
}
return (double)((double)(ind1el + ind2el)) / 2.0;
}
Optimal
#include <bits/stdc++.h>
double median(vector<int>& a, vector<int>& b) {
//size of two given arrays:
int n1 = a.size(), n2 = b.size();
// first array is always the smaller array for less TC
if(n1>n2) return median(b,a);
int low=0;
int high=n1;
int left = (n1+n2+1)/2;
int n =n1+n2;
while (low <= high) {
int mid1 = (low + high) >> 1;
int mid2 = left - mid1;
int l1 = INT_MIN;
int l2 = INT_MIN;
int r1 = INT_MAX;
int r2 = INT_MAX;
if (mid1 < n1)
r1 = a[mid1];
if (mid2 < n2)
r2 = b[mid2];
if (mid1 - 1 >= 0)
l1 = a[mid1 - 1];
if (mid2 - 1 >= 0)
l2 = b[mid2 - 1];
if (l1 <= r2 && l2 <= r1) {
if(n%2==1) return max(l1,l2);
else return ((double)(max(l1, l2) + min(r1, r2))) / 2.0;
} else if (l1 > r2)
high = mid1 - 1;
else
low = mid1 + 1;
}
return 0;
}