Median of Two Sorted Arrays in C++

Suppose we have two arrays; these arrays are sorted. So we have to find the median of these two arrays. So if the arrays are like [1,5,8] and [2,3,6,9], then the answer will be 5.
To solve this, we will follow these steps −
Define a function findMedianSortedArrays, this will take nums1 and nums2 arrays
Define a function findMedianSortedArrays, this will take nums1 and nums2 arrays
if size of nums1 > size of nums2, then,Call the function return findMedianSortedArrays(nums2, nums1)
if size of nums1 > size of nums2, then,
Call the function return findMedianSortedArrays(nums2, nums1)
Call the function return findMedianSortedArrays(nums2, nums1)
x := size of nums1, y := size of nums2
x := size of nums1, y := size of nums2
low := 0, high := x
low := 0, high := x
totalLength := x + y
totalLength := x + y
while low<=high, do −partitionX := low + (high - low) / 2partitionY := (totalLength + 1) / 2 - partitionXmaxLeftX = -inf when partitionX is 0, otherwise nums1[partitionX-1]maxRightX = inf when partitionX is x, otherwise nums1[partitionX]maxLeftY = -inf when partitionY is 0, otherwise nums2[partitionY-1]maxRightY = inf when partitionY is y, otherwise nums2[partitionY]if maxLeftX<=minRightY and maxLeftY <= minRightX, then,if totalLength mod 2 is same as 0, then,return (maximum of maxLeftX and maxLeftY) + minimum of minRightX and minRightY) / 2Otherwisereturn maximum of maxLeftX and maxLeftYOtherwise when maxLeftX>minRightY, then −high := partitionX - 1Otherwise low := partitionX + 1
while low<=high, do −
partitionX := low + (high - low) / 2
partitionX := low + (high - low) / 2
partitionY := (totalLength + 1) / 2 - partitionX
partitionY := (totalLength + 1) / 2 - partitionX
maxLeftX = -inf when partitionX is 0, otherwise nums1[partitionX-1]
maxLeftX = -inf when partitionX is 0, otherwise nums1[partitionX-1]
maxRightX = inf when partitionX is x, otherwise nums1[partitionX]
maxRightX = inf when partitionX is x, otherwise nums1[partitionX]
maxLeftY = -inf when partitionY is 0, otherwise nums2[partitionY-1]
maxLeftY = -inf when partitionY is 0, otherwise nums2[partitionY-1]
maxRightY = inf when partitionY is y, otherwise nums2[partitionY]
maxRightY = inf when partitionY is y, otherwise nums2[partitionY]
if maxLeftX<=minRightY and maxLeftY <= minRightX, then,if totalLength mod 2 is same as 0, then,return (maximum of maxLeftX and maxLeftY) + minimum of minRightX and minRightY) / 2Otherwisereturn maximum of maxLeftX and maxLeftY
if maxLeftX<=minRightY and maxLeftY <= minRightX, then,
if totalLength mod 2 is same as 0, then,return (maximum of maxLeftX and maxLeftY) + minimum of minRightX and minRightY) / 2
if totalLength mod 2 is same as 0, then,
return (maximum of maxLeftX and maxLeftY) + minimum of minRightX and minRightY) / 2
return (maximum of maxLeftX and maxLeftY) + minimum of minRightX and minRightY) / 2
Otherwisereturn maximum of maxLeftX and maxLeftY
Otherwise
return maximum of maxLeftX and maxLeftY
return maximum of maxLeftX and maxLeftY
Otherwise when maxLeftX>minRightY, then −high := partitionX - 1
Otherwise when maxLeftX>minRightY, then −
high := partitionX - 1
high := partitionX - 1
Otherwise low := partitionX + 1
Otherwise low := partitionX + 1
return 0
return 0
Let us see the following implementation to get better understanding −
 Live Demo
#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
   double findMedianSortedArrays(vector& nums1, vector<int>& nums2) {
      if(nums1.size()>nums2.size())
         return findMedianSortedArrays(nums2,nums1);
      int x = nums1.size();
      int y = nums2.size();
      int low = 0;
      int high = x;
      int totalLength = x+y;
      while(low<=high){
         int partitionX = low + (high - low)/2;
         int partitionY = (totalLength + 1)/2 - partitionX;
         int maxLeftX = (partitionX ==0?INT_MIN:nums1[partitionX1] );
         int minRightX = (partitionX == x?INT_MAX :
         nums1[partitionX]);
         int maxLeftY = (partitionY ==0?INT_MIN:nums2[partitionY1] );
         int minRightY = (partitionY == y?INT_MAX : nums2[partitionY]);
         if(maxLeftX<=minRightY && maxLeftY <= minRightX){
            if(totalLength% 2 == 0){
               return ((double)max(maxLeftX,maxLeftY) + (double)min(minRightX,minRightY))/2;
            } else {
               return max(maxLeftX, maxLeftY);
            }
         }
         else if(maxLeftX>minRightY)
            high = partitionX-1;
         else low = partitionX+1;
      }
      return 0;
   }
};
main(){
   Solution ob;
   vector<int> v1 = {1,5,8}, v2 = {2,3,6,9};
   cout << (ob.findMedianSortedArrays(v1, v2));
}
[1,5,8]
[2,3,6,9]
5