Sum of Middle Elements of two sorted arrays

QUESTION

Given 2 sorted arrays A and B of size N each. Print sum of middle elements of the array obtained after merging the given arrays.\n\nInput:\nThe first line contains ‘T’ denoting the number of testcases. Then follows description of testcases.\nEach case begins with a single positive integer N denoting the size of array.\nThe second line contains the N space separated positive integers denoting the elements of array A.\nThe third line contains N space separated positive integers denoting the elements of array B.\n \n\nOutput:\n\nFor each testcase, print the sum of middle elements of two sorted arrays. The number of the elements in the merged array are even so there will be 2 numbers in the center n/2 and n/2 +1. You have to print their sum. \n\n\nConstraints:\n 1<=T<=50\n 1<=N<=1000\n 1<=A[i]<=100000\n 1<=B[i]<=100000.

“TESTCASE_1”: “1\n5\n1 2 4 6 10\n4 5 6 9 12\n###—###SEPERATOR—###—\n11”, “TESTCASE_2”: “1\n5\n0 2 4 6 8 \n1 3 5 7 9\n###—###SEPERATOR—###—\n9\n”, “TESTCASE_3”: “0\n###—###SEPERATOR—###—\n0”, “TESTCASE_4”: “0\n###—###SEPERATOR—###—\n0”, “TESTCASE_5”: “0\n###—###SEPERATOR—###—\n0

ANSWER

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int main(){
 int t,n,val;
 cin>>t;
 while(t--){
  
  cin>>n;
  int a[n],b[n];
  vector<int>c(2*n);
  for(int i=0;i<n;i++){
   cin>>a[i];
  }
  
  for(int i=0;i<n;i++){
   cin>>b[i];
  }
  merge(a,a+n,b,b+n,c.begin());
  int s=c.size();
  cout<<(c[(s/2)-1]+c[s/2])<<endl;
  
 }
}
Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Powered By
CHP Adblock Detector Plugin | Codehelppro