Median of 2 Sorted Arrays of Different Sizes | Practice | GeeksforGeeks

Given two sorted arrays array1 and array2 of size m and n respectively. Find the median of the two sorted arrays.
Example 1:
Input:
m = 3, n = 4
array1[] = {1,5,9}
array2[] = {2,3,6,7}
Output: 5
Explanation: The middle element for
{1,2,3,5,6,7,9} is 5

Example 2:
Input:
m = 2, n = 4
array1[] = {4,6}
array2[] = {1,2,3,5}
Output: 3.5
Your Task:
The task is to complete the function MedianOfArrays() that takes array1 and array2 as input and returns their median. 

Can you solve the problem in expected time complexity?
Expected Time Complexity: O(min(log n, log m)).
Expected Auxiliary Space: O((n+m)/2).
Constraints: 
0 ≤ m,n ≤ 104
1 ≤ array1[i], array2[i] ≤ 105
0
adarshsharadpandey23in 12 hours
vector<int> arr;
        for(int i=0;i<array1.size();i++)
        {
            arr.push_back(array1[i]);
        }
        for(int i=0;i<array2.size();i++)
        {
            arr.push_back(array2[i]);
        }
        sort(arr.begin(),arr.end());
        if(arr.size()%2==0)
        {
            return ((double)arr[arr.size()/2] + (double)arr[(arr.size()/2)-1])/2;
        }
        return arr[(arr.size()/2)];
0
parmeet4506 days ago
static double medianOfArrays(int n, int m, int a[], int b[])    {       // Your Code Here       ArrayList<Integer> al = new ArrayList<Integer>();       int nc=0;       int mc=0;       while(nc<n && mc<m){           if(a[nc]>b[mc]){               al.add(b[mc]);               mc++;           }           else{               al.add(a[nc]);               nc++;           }       }       if(mc<m){           for(int i=mc;i<m;i++){               al.add(b[i]);           }       }else if(nc<n){           for(int i=nc;i<n;i++){               al.add(a[i]);           }       }       int index = (n+m)/2;       if((n+m)%2 == 1){           return Double.parseDouble(al.get(index)+"");       }else{           return (Double.parseDouble(al.get(index)+"")+Double.parseDouble(al.get(index-1)+""))/2;       }   }
0
rabab0771 week ago
Easy 10 line Python Solution:
list contains all elements of both arrays, sort it, compute length of this list.
list contains all elements of both arrays, sort it, compute length of this list.
#If len(z) is even:
---Calculate x = sum of mid term and the term before the mid term.
---The sum of two middle terms divided by 2 will give their average.
---If average is an even number, divide by 2 and return only floor value.
---if average is odd, return decimal value as well, so simply divide by 2
#if len(z) is odd:
---just return the term at mid term (length divide by 2)   def MedianOfArrays(self, array1, array2):           #code here           z = list(array1+array2)            z.sort()            if len(z)%2==0:                x = z[len(z)//2]+z[len(z)//2 - 1]               if x%2==0:                   return x//2               else:                   return x/2           else:               return z[len(z)//2]
please upvote! thanks :)
0
anilkumarreddy100620002 weeks ago
double MedianOfArrays(vector<int>& array1, vector<int>& array2)   {       int size1=array1.size();       int size2=array2.size();       int arr[size1+size2];       int i=0,j=0,k=0;       while(i<size1 && j<size2){           if(array1[i]<array2[j]){               arr[k++]=array1[i++];           }else{               arr[k++]=array2[j++];           }       }       while(i<size1) arr[k++]=array1[i++];       while(j<size2) arr[k++]=array2[j++];      int n=size1+size2;        if(n%2==0){          double a=(arr[n/2]+arr[n/2-1])/2.0;          return a;        }else{            return arr[n/2];        }       return 0;      }
0
mayank180919992 weeks ago
    double MedianOfArrays(vector<int>& array1, vector<int>& array2)
    {
        // Your code goes here
         vector<int>v;
         for(int i=0;i<array1.size();i++){
             v.push_back(array1[i]);
         }
         for(int i=0;i<array2.size();i++){
             v.push_back(array2[i]);
         }
         sort(v.begin(),v.end());
         int n=v.size();
         if(n%2==0){
           double a=(v[n/2]+v[n/2-1])/2.0;
           return a;
         }else{
             return v[n/2];
         }
    return 0;
    }
-1
yadavkhushwant7772 weeks ago
---JAVA SOLUTION---
 
class GFG {    static double medianOfArrays(int n, int m, int a[], int b[])    {       // Your Code Here       double median;       int medIndex;       int newArr[] = new int[n+m];       for (int i=0; i<n; i++){           newArr[i] = a[i];       }       for (int i=0; i<m; i++){           newArr[n+i] = b[i];       }
       Arrays.sort(newArr);       medIndex = newArr.length/2;       if(newArr.length%2 == 0){           double temp = newArr[medIndex-1]+newArr[medIndex];           median = temp/2;       } else median = newArr[medIndex];              return median;   }}
0
rainx3 weeks ago
If you try to memorise it, you are doing wrong
+2
anandgeeky3 weeks ago
JAVA CODE:
 
       int[] arr = new int[n + m];       // Merge two array into one array       System.arraycopy(a, 0, arr, 0, n);       System.arraycopy(b, 0, arr, n, m);       // Sort the merged array       Arrays.sort(arr);              int size = arr.length;             // If length of array is even       if (size % 2 == 0)       {           int z = size / 2;           int e = arr[z];           int q = arr[z - 1];           double ans =(double) (e + q) / 2;           return ans;       }             // If length if array is odd       else       {           int z = Math.round(size / 2);           return arr[z];       }
0
ritishrish1813 weeks ago
 double MedianOfArrays(vector<int>& array1, vector<int>& array2)
    {
        // Your code goes here
        vector<int> v;
        for(int  i=0;i<array1.size();i++){
            v.push_back(array1[i]);
        }
        for(int i=0;i<array2.size();i++){
            v.push_back(array2[i]);
        }
        sort(v.begin(),v.end());
        int n=array1.size()+array2.size();
        if(n%2==0){
            double a=((double)v[n/2]+(double)v[n/2-1])/2;
            return a;
        }
        else{
            double a=(double)v[n/2];
            return a;
        }
0
ritishrish181
This comment was deleted.
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.