You are given two arrays A and B each containing N numbers. You need to choose exactly one number from A and exactly one number from B such that the index of the two chosen numbers is not same and the sum of the 2 chosen values is minimum. Formally, if you chose ith element from A whose value is x and jth element from B whose value is y, you need to minimize the value of (x+y) such that i is not equal to j.
Your objective is to find this minimum value.
Input:
The first line of input contains an integer denoting the test cases,t. The first line of each test case contains an integer N denoting the size of two arrays. Then each of the next two lines contains N space separated integers denoting values of the array A[] and B[] respectively.
Output:
Print the minimum sum which can be obtained under the conditions mentioned in the problem statement.If not possible print "-1" without quotes.
Constraints:
1<=T<=30
1<= N <=100000
1< =Array elements < =100000
Example:
Input:
1
5
5 4 3 2 1
1 2 3 4 5
Output:
2
Explanation:
Minimum sum will be obtained by choosing number at the last index of first array i.e. 5th element of the first array(1) and first index of the second array ie first element of second array (1).
Sum=1+1=2 as their indexes are different but sum is minimum.
What I have tried:
#include
int main()
{
int t,i,j,k,min=0;
scanf("%d",&t);
struct testcase
{
int sizeOfArray;
int a[10];
int b[10];
int ans;
};
struct testcase tc[t];
for(i=0;i<t;i++)
{
scanf("%d",&tc[i].sizeOfArray);
for(j=0;j<tc[i].sizeOfArray;j++) {
scanf("%d",&(tc[i].a[j]));
}
for(j=0;j<tc[i].sizeOfArray;j++) {
scanf("%d",&tc[i].b[j]);
}
}
int no=0;
for(k=0;k<t;k++)
{
min= tc[k].a[0]+tc[k].b[1];
for(i=0;i<tc[k].sizeOfArray;i++)
{
for(j=0;(j<tc[k].sizeOfArray);j++)
{
if((tc[k].a[i]+tc[k].b[j]<min)&&(j!=i))
{
min=tc[k].a[i]+tc[k].b[j];
}
}
}
tc[k].ans=min;
printf("%d\n",min);
}
return 0;
}
//As this is giving correct results on my system but I am getting runtime error while //submitting this code