Sum of Middle Elements of two sorted arrays | Practice | GeeksforGeeks

Given 2 sorted arrays Ar1 and Ar2 of size N each. Merge the given arrays and find the sum of the two middle elements of the merged array.
 
Example 1:
Input:
N = 5
Ar1[] = {1, 2, 4, 6, 10}
Ar2[] = {4, 5, 6, 9, 12}
Output: 11
Explanation: The merged array looks like
{1,2,4,4,5,6,6,9,10,12}. Sum of middle
elements is 11 (5 + 6).

 
Example 2:
Input:
N = 5
Ar1[] = {1, 12, 15, 26, 38}
Ar2[] = {2, 13, 17, 30, 45}
Output: 32
Explanation: The merged array looks like
{1, 2, 12, 13, 15, 17, 26, 30, 38, 45} 
sum of middle elements is 32 (15 + 17).
 
Your Task:
You don't need to read input or print anything. Your task is to complete the function findMidSum() which takes  ar1, ar2 and n as input parameters and returns the sum of middle elements. 
 
Expected Time Complexity: O(log N)
Expected Auxiliary Space: O(1)
 
Constraints:
1 <= N <= 103
1 <= Ar1[i] <= 106
1 <= Ar2[i] <= 106
0
suprithsk200116 hours ago
//simple c++ soln
vector<int> ans;           for(int i=0;i<n;i++){               ans.push_back(ar1[i]);           }           for(int i=0;i<n;i++){               ans.push_back(ar2[i]);           }           sort(ans.begin(),ans.end());           int mid=ans.size()/2;           return ans[mid]+ans[mid-1];
0
tupesanket199919 hours ago
Expected Time Complexity: O(log N)
Expected Auxiliary Space: O(1)
    int findMidSum(int ar1[], int ar2[], int n) {
        int low = 0,high=n;
        while(low <= high){
            int cut1 = (low + high)>>1 ;
            int cut2 = n - cut1;
            int left1 =  (cut1==0) ? INT_MIN:ar1[cut1-1];
            int left2 =  (cut2==0) ? INT_MIN:ar2[cut2-1];
            int right1 = (cut1==n) ? INT_MAX:ar1[cut1];
            int right2 = (cut2==n) ? INT_MAX:ar2[cut2];
            if(left1<= right2 && left2 <=right1){
                return max(left1,left2) + min(right1,right2);
            }else if(left1 > right2){
                high = cut1-1;
            }else{
                low = cut1+1;
            }
        }
        return -1;
    }
0
sambhavjsj20001 week ago
PYTHON
TIME TAKEN - 0.21
class Solution:def findMidSum(self, ar1, ar2, n):  li=ar1+ar2       mid=len(li)//2       middle=[]       sum=0       li=sorted(li)       if len(li)%2!=0:           middle.append(li[mid])       else:           middle.append(li[mid-1])           middle.append(li[mid])       for i in middle:           sum+=i       return sum
0
mayank180919991 week ago
 int findMidSum(int ar1[], int ar2[], int n) {
            // code here
            vector<int>ans;
            for(int i=0;i<n;i++){
                ans.push_back(ar1[i]);
                ans.push_back(ar2[i]);
            }
            sort(ans.begin(),ans.end());
            return ans[n]+ans[n-1];
    }
0
mayank18091999
This comment was deleted.
0
shaikhmohammedammar1 week ago
int findMidSum(int ar1[], int ar2[], int n) {
            vector<int> arr;
            /* /// trying for pointers, but can't help.
            int pt1 = 0, pt2 = 0;
            for(int i=0; i<n-1; i++){
                if(ar1[pt1]<ar2[pt2] and pt1<n) pt1++;
                else pt2++;
            }
            return (ar1[pt1]+ar2[pt2]);
            */
            for(int i=0; i<n; i++){
                arr.push_back(ar1[i]);
                arr.push_back(ar2[i]);
            }
            n =  arr.size();
            sort(arr.begin(), arr.end());
            return (arr[n>>1]+arr[(n>>1)-1]);
    }
+1
aloksinghbais021 week ago
C++ solution having time complexity as O(2*2*log(N)*log(N)) and space complexity as O(1) is as follows :- 
 
Execution Time :- 0.21 / 1.91 sec
 
int helper(int ar1[],int ar2[],int n,int ele){       int s = 0;       int e = n-1;       while(s <= e){           int mid = (s+e)/2;           int lb = lower_bound(ar2,ar2+n,ar1[mid]) - ar2;           int ub = upper_bound(ar2,ar2+n,ar1[mid]) - ar2;                      int lbne = lb + mid;           int ubne = ub + mid;           if(ele >= lbne && ele <= ubne){               return (ar1[mid]);           }           else if(ele < lbne){               e = mid - 1;           }           else{               s = mid + 1;           }       }              s = 0;       e = n-1;       while(s <= e){           int mid = (s+e)/2;           int lb = lower_bound(ar1,ar1+n,ar2[mid]) - ar1;           int ub = upper_bound(ar1,ar1+n,ar2[mid]) - ar1;                      int lbne = lb + mid;           int ubne = ub + mid;           if(ele >= lbne && ele <= ubne){               return (ar2[mid]);           }           else if(ele < lbne){               e = mid - 1;           }           else{               s = mid + 1;           }       }              return (-1);   }      int findMidSum(int ar1[], int ar2[], int n) {       int a = helper(ar1,ar2,n,n-1);       int b = helper(ar1,ar2,n,n);       return (a+b);   }
0
imabhishek021 week ago
easy solution c++:
int findMidSum(int ar1[], int ar2[], int n) {
        
       vector<int >ans;
       
       for(int i=0;i<n;i++)
       {
           ans.push_back(ar1[i]);
           ans.push_back(ar2[i]);
       }
       sort(ans.begin(),ans.end());
       
       int x=ans.size();
       return ans[x/2]+ans[(x/2)-1];
      
       
    }
};
0
kiranbari12 weeks ago
int findMidSum(int ar1[], int ar2[], int n){
       int i=0,j=0,c=0;
       int n1,n2;
       while(c<=n){
           if(j==n ||(i<n && j<n && ar1[i]<=ar2[j])){
               if(c==n-1)
                   n1=ar1[i];
               else if(c==n){
                   n2=ar1[i];
               }
               i++;
           }else if(i==n ||(i<n && j<n && ar1[i]>ar2[j])){
               if(c==n-1)
                   n1=ar2[j];
               else if(c==n){
                   n2=ar2[j];
               }
               j++;
           }
           c++;
       }
       return n1+n2;
   }
+1
aashaykaurav2 weeks ago
python -
def findMidSum(self, ar1, ar2, n):  # code here  Arr = [] i ,j = 0, 0 while i < n and j < n:          if ar1[i] <= ar2[j]:         Arr.append(ar1[i])         i += 1     else:         Arr.append(ar2[j])         j += 1  if i == n:     while j < n:         Arr.append(ar2[j])         j += 1          if j == n:     while i < n:         Arr.append(ar1[i])            i += 1             k = len(Arr)//2 s = Arr[k] + Arr[k-1] return s
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.