Skip to content

Commit 75ed6d5

Browse files
committed
Merge branch 'dev' of github.com:akgmage/data-structures-and-algorithms into dev
2 parents 513d763 + e54a2a9 commit 75ed6d5

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
Approach:
3+
let nums1 = [1, 3, 4, 7, 10, 12], nums2 = [2, 3, 6, 15]
4+
5+
In order to find the median, we need a single sorted array. So a naive approach is that, just merge the 2 sorted arrays and find the median of that array.
6+
This will have a time Complexity of O(n1 + n2), Space Complexity of O(n1 + n2)
7+
8+
Now, lets optimise it.
9+
So, the sorted form of the given array is [1, 2, 3, 3, 4, 6, 7, 10, 12, 15]. To find the median of the array, we need to select the 2 mid elements and average it out. Now suppose, on partitioning the above merged-sorted array in the mid-point, we get 5 elements on left and 5 elements on the right.
10+
Now, we can get 5 elements by selecting {4 from left, 1 from right}, {3 from left, 2 from right}, {2 from left, 3 from right} and {1 from left, 4 from right}.
11+
Lets analyse case-wise:
12+
case 1: 4 from left, 1 from right
13+
14+
*/
15+
16+
#include<bits/stdc++.h>
17+
18+
class Solution {
19+
public:
20+
double findMedianSortedArrays(std::vector<int>& nums1, std::vector<int>& nums2) {
21+
int n1 = nums1.size(); // stores the length of nums1 array
22+
int n2 = nums2.size(); // stores the length of nums2 array
23+
24+
if(n2 > n1) return findMedianSortedArrays(nums2, nums1);
25+
26+
// according to approach described above, I am applying binary search on nums1 array
27+
int lo = 0, hi = n1;
28+
while(lo <= hi){
29+
int mid1 = (lo+hi)/2; // mid of nums1
30+
int mid2 = (n1 + n2 + 1)/2 - mid1;
31+
// why?? => suppose nums1[] is partitioned at index 3 => nums1[3] = 7.
32+
33+
std::pair<int, int> maxleft, maxright;
34+
maxleft.first = mid1 == 0 ? INT_MIN : nums1[mid1-1];
35+
maxleft.second = mid2 == 0 ? INT_MIN : nums2[mid2-1];
36+
37+
minright.first = mid1 == n1 ? INT_MAX : nums1[mid1];
38+
minright.second = mid2 == n2 ? INT_MAX : nums2[mid2];
39+
40+
if(maxleft.first <= minright.second and maxleft.second <= minright.first){
41+
if((n1+n2)%2 == 1){
42+
return std::max(maxleft.first, maxleft.second);
43+
} else {
44+
return (std::max(maxleft.first, maxleft.second) + std::min(minright.first, minright.second))/2.0;
45+
}
46+
} else if (maxleft.first > minright.second){
47+
hi = mid1-1;
48+
} else {
49+
lo = mid1+1;
50+
}
51+
}
52+
}
53+
};
54+
55+
int main(int argc, char const *argv[])
56+
{
57+
int n;
58+
59+
std::cin>>n;
60+
std::vector<int> arr1(n, 0);
61+
for(int i=0;i<n;i++){
62+
std::cin>>arr1[i];
63+
}
64+
65+
std::cin>>n;
66+
std::vector<int> arr2(n, 0);
67+
for(int i=0;i<n;i++){
68+
std::cin>>arr2[i];
69+
}
70+
71+
const Solution sol = new Solution();
72+
double res = sol.findMedianSortedArrays(arr1, arr2);
73+
74+
std::cout<<res<<"\n";
75+
return 0;
76+
}

0 commit comments

Comments
 (0)