Sum of Middle Elements of two sorted arrays
PROBLEM :
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.
Input:
The first line contains 'T' denoting the number of testcases. Then follows description of testcases.
Each case begins with a single positive integer N denoting the size of array.
The second line contains the N space separated positive integers denoting the elements of array A.
The third line contains N space separated positive integers denoting the elements of array B.
Output:
For 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.
Constraints:
1<=T<=50
1<=N<=1000
1<=A[i]<=100000
1<=B[i]<=100000
Example:
Input :
1
5
1 2 4 6 10
4 5 6 9 12
Output :
11
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
#define max 1000
int Middle_Elements_sum(int [],int [],int ) ;
int main()
{
int t,arr1[max],arr2[max],no,i ;
cin>>t ;
while(t--)
{
cin>>no ;
for(i=0;i<no;i++)
cin>>arr1[i] ;
for(i=0;i<no;i++)
cin>>arr2[i] ;
cout<<Middle_Elements_sum(arr1,arr2,no)<<endl ;
}
return 0;
}
int Middle_Elements_sum(int arr1[],int arr2[],int no)
{
int i,j,k ;
k=no-1 ;
i=0 ;
j=0 ;
while(k)
{
if(arr1[i]==arr2[j])
{
i++ ;
j++ ;
k-- ;
k-- ;
}
else if(arr1[i]<arr2[j])
{
i++ ;
k-- ;
}
else
{
j++ ;
k-- ;
}
}
if(arr1[i]<arr2[j]&&arr1[i+1]<arr2[j])
return arr1[i]+arr1[i+1] ;
if(arr1[i]>arr2[j]&&arr1[i]>arr2[j+1])
return arr2[j]+arr2[j+1] ;
return arr1[i]+arr2[j] ;
}
---------------------------------------------------------------------------------
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.
Input:
The first line contains 'T' denoting the number of testcases. Then follows description of testcases.
Each case begins with a single positive integer N denoting the size of array.
The second line contains the N space separated positive integers denoting the elements of array A.
The third line contains N space separated positive integers denoting the elements of array B.
Output:
For 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.
Constraints:
1<=T<=50
1<=N<=1000
1<=A[i]<=100000
1<=B[i]<=100000
Example:
Input :
1
5
1 2 4 6 10
4 5 6 9 12
Output :
11
--------------------------------------------------------------------------------
SIMPLE c++ IMPLEMENTATION :
--------------------------------------------------------------------------------
#include<iostream>
using namespace std;
#define max 1000
int Middle_Elements_sum(int [],int [],int ) ;
int main()
{
int t,arr1[max],arr2[max],no,i ;
cin>>t ;
while(t--)
{
cin>>no ;
for(i=0;i<no;i++)
cin>>arr1[i] ;
for(i=0;i<no;i++)
cin>>arr2[i] ;
cout<<Middle_Elements_sum(arr1,arr2,no)<<endl ;
}
return 0;
}
int Middle_Elements_sum(int arr1[],int arr2[],int no)
{
int i,j,k ;
k=no-1 ;
i=0 ;
j=0 ;
while(k)
{
if(arr1[i]==arr2[j])
{
i++ ;
j++ ;
k-- ;
k-- ;
}
else if(arr1[i]<arr2[j])
{
i++ ;
k-- ;
}
else
{
j++ ;
k-- ;
}
}
if(arr1[i]<arr2[j]&&arr1[i+1]<arr2[j])
return arr1[i]+arr1[i+1] ;
if(arr1[i]>arr2[j]&&arr1[i]>arr2[j+1])
return arr2[j]+arr2[j+1] ;
return arr1[i]+arr2[j] ;
}
---------------------------------------------------------------------------------
Comments
Post a Comment