-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathMedian_of_TwoSortedArrays_OptimisedCode.cpp
83 lines (67 loc) · 2.23 KB
/
Median_of_TwoSortedArrays_OptimisedCode.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Most Optimised and Efficient approach to find the median of Two sorted Arrays of different sizes
#include <bits/stdc++.h>
using namespace std;
// Binary Search Method to find median
double MedianOfSortedArrays(vector<int>& nums1, vector<int>& nums2)
{
if(nums2.size() < nums1.size()) return MedianOfSortedArrays(nums2,nums1);
// Swapping to make first array i.e. nums1 smaller
int n1 = nums1.size();
int n2 = nums2.size();
int low = 0;
int high= n1;
while (low <= high)
{
int cut1 = (low+high) >> 1; // (low+high)/2
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 correct partition is done
if (left1<= right2 and 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;
}
// Driver code
int main()
{
cout<<"Enter the size of first array"<<endl;
int n1;
cin>>n1;
vector<int> arr1 ;
int a;
cout<<"Enter the elements of First array"<<endl;
for(int i=0; i<n1; i++)
{
cin>>a;
arr1.push_back(a);
}
cout<<"Enter the size of second array"<<endl;
int n2;
cin>>n2;
vector<int> arr2;
cout<<"Enter the elements of Second array"<<endl;
for(int i=0; i<n2; i++)
{
cin>>a;
arr2.push_back(a);
}
cout << "Median of the two arrays are" << endl;
cout << MedianOfSortedArrays(arr1, arr2);
return 0;
// Time Complexity: O(min(log n1, log n2)) [Since binary search is being applied on the smaller of the 2 arrays]
// Auxiliary Space: O(1) [ As no extra space is used ]
}