I Will give a good solution soon after


  • 0
    Z
      class Solution {
        public:
      double findMedianSortedArrays(vector<int>& nums1, vector<int>& nums2) {
    
    if(nums1.empty() && nums2.empty())
        return 0.0;
    
    vector<int> v1,v2,v;
    v1 = nums1;
    v2 = nums2;
    int n = v1.size();
    int m = v2.size();
    double median = 0;
    int index1 = 0;
    int index2 = 0;
    
        while(index1 < n && index2 < m)
        {
            if(v1[index1] < v2[index2])
            {
                v.push_back(v1[index1]);
                index1++;
            }
            else
            {
                v.push_back(v2[index2]);
                index2++;
            }
        }
        while(index1 < n)
        {
            v.push_back(v1[index1]);
            index1++;
        }
        while(index2 < m)
        {
            v.push_back(v2[index2]);
            index2++;
        }
    
        if(v.size() % 2 == 0)
        {
            median = (double)(v[v.size() / 2] + v[v.size() / 2 - 1]) / 2;
        }
        if(v.size() % 2 != 0)
        {
            median = (double)v[v.size() / 2];
        }
        return median;
    }
     };

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.