Timer is Running

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.

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.

abnandi04 | 801 |

KshamaGupta | 412 |

srivastavad270 | 223 |

bhavyapratap56 | 206 |

anshusingh20112001 | 181 |

abnandi04 | 776 |

KshamaGupta | 296 |

srivastavad270 | 223 |

bhavyapratap56 | 206 |

afreensartaj131 | 170 |

blackshadows | 5362 |

Ibrahim Nash | 5244 |

akhayrutdinov | 5111 |

mb1973 | 4976 |

Quandray | 4748 |

Login to report an issue on this page.