Advertisement
i_love_rao_khushboo

Median of Two Sorted Arrays

Apr 4th, 2022
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.97 KB | None | 0 0
  1. // Explanation: https://www.youtube.com/watch?v=ws98ud9uxl4
  2. //              https://www.youtube.com/watch?v=LPFhl65R7ww
  3. //              https://takeuforward.org/data-structure/median-of-two-sorted-arrays-of-different-sizes/
  4. //              https://leetcode.com/problems/median-of-two-sorted-arrays/discuss/2481/share-my-ologminmn-solution-with-explanation
  5.  
  6. // Problem: https://www.interviewbit.com/problems/median-of-array/
  7. //          https://leetcode.com/problems/median-of-two-sorted-arrays/
  8. /****************************************************************************************************/
  9.  
  10. #include<bits/stdc++.h>
  11. using namespace std;
  12.  
  13. #define ll long long
  14. #define ull unsigned long long
  15. #define pb push_back
  16. #define mp make_pair
  17. #define F first
  18. #define S second
  19. #define PI 3.1415926535897932384626
  20. #define deb(x) cout << #x << "=" << x << endl
  21. #define deb2(x, y) cout << #x << "=" << x << ", " << #y << "=" << y << endl
  22. typedef pair<int, int> pii;
  23. typedef pair<ll, ll> pll;
  24. typedef vector<int> vi;
  25. typedef vector<ll> vll;
  26. typedef vector<ull> vull;
  27. typedef vector<pii> vpii;
  28. typedef vector<pll> vpll;
  29. typedef vector<vi> vvi;
  30. typedef vector<vll> vvll;
  31. typedef vector<vull> vvull;
  32. mt19937_64 rang(chrono::high_resolution_clock::now().time_since_epoch().count());
  33. int rng(int lim) {
  34.     uniform_int_distribution<int> uid(0,lim-1);
  35.     return uid(rang);
  36. }
  37.  
  38. const int INF = 0x3f3f3f3f;
  39. const int mod = 1e9+7;
  40.  
  41. double find_median(vi &v1, vi &v2) {
  42.     int n = (int)v1.size();
  43.     int m = (int)v2.size();
  44.    
  45.     // Ensuring that v1.length() <= v2.length()
  46.     if(n > m) return find_median(v2, v1);
  47.    
  48.     int start = 0, end = n;
  49.        
  50.     while(start <= end) {
  51.         int i1 = (start + end) / 2;
  52.         int i2 = (n + m + 1) / 2 - i1;
  53.            
  54.         int min1 = (i1 == n) ? INT_MAX : v1[i1];
  55.         int max1 = (i1 == 0) ? INT_MIN : v1[i1-1];
  56.            
  57.         int min2 = (i2 == m) ? INT_MAX : v2[i2];
  58.         int max2 = (i2 == 0) ? INT_MIN : v2[i2-1];
  59.            
  60.         if((max1 <= min2) and (max2 <= min1)) {
  61.             if((n + m) % 2 == 0) {
  62.                 return ((double)max(max1, max2) + min(min1, min2)) / 2;
  63.             }
  64.                
  65.             else return (double)max(max1, max2);
  66.         }
  67.            
  68.         else if(max1 > min2) end = i1 - 1;
  69.         else start = i1 + 1;
  70.     }
  71.    
  72.     // Only we we can come here is if input arrays were not sorted.
  73.     return 0.0;
  74. }
  75.  
  76. void solve()
  77. {
  78.     int n, m; cin >> n >> m;
  79.     vi v1(n), v2(m);
  80.    
  81.     for(int i = 0; i < n; i++) cin >> v1[i];
  82.     for(int i = 0; i < m; i++) cin >> v2[i];
  83.    
  84.     cout << find_median(v1, v2);
  85. }
  86.  
  87. int main()
  88. {
  89.     ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
  90.     srand(chrono::high_resolution_clock::now().time_since_epoch().count());
  91.  
  92.     // #ifndef ONLINE_JUDGE
  93.     //     freopen("input.txt", "r", stdin);
  94.     //     freopen("output.txt", "w", stdout);
  95.     // #endif
  96.  
  97.     int t = 1;
  98.     // int test = 1;
  99.     // cin >> t;
  100.     while(t--) {
  101.       // cout << "Case #" << test++ << ": ";
  102.       solve();
  103.     }
  104.  
  105.     return 0;
  106. }
  107.  
  108. // Time complexity: O(log(min(m,n))
  109. // Space complexity: O(1)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement