Minimums in Array
Submissions: 4554   Accuracy:

16.45%

  Difficulty: Medium   Marks: 4

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.
 

** For More Input/Output Examples Use 'Expected Output' option **

Contributor: vaibhav garg
Author: justrelax


If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.



Need help with your code? Please use ide.geeksforgeeks.org, generate link and share the link here.


to report an issue on this page.