ACPC11B - Between the Mountains

Did you try to ride a telepherique (cable car)? It is a lot of fun. Our company is trying to build a new telepherique line between two high mountains and you will be invited for a free ride. The trick to get your free ride is to help the company in building the telepherique line.

The company wants to build two platforms, one on each mountain. The line will extend between these two platforms. The suitable points for holding a platform in each mountain were determined, and the altitudes of these points were reported.

One of the talented engineers suggested that the company can save a lot of energy if the two stations have the same altitude or at least have altitudes which are as close to each other as possible. Your job is to select two points, one at each mountain, from those suitable points, so that the altitude difference between the two points is as little as possible.

Input

Your program will be tested on one or more test cases. The first line of the input will be a single integer T , the number of test cases (1 ≤ T ≤ 100). Follows 2T lines containing the test cases, each on a pair of lines.

Each of the two lines in a case describe the altitudes of suitable points to build a platform on one mountain. Each line starts with an integer N , the number of reported altitudes (1 ≤ N ≤ 1,000), followed by N integers, which represent the altitudes reported from this mountain. Any altitude value will be between 1 and 1,000,000, inclusive.

Output

For each test case, output, on a single line, a single number representing the minimum

altitude difference between two suitable platform points, one at each mountain.

Example

Input:
2
8 1 3 5 7 9 7 3 1
8 2 4 6 8 10 8 6 2
8 2 3 5 10 9 3 2 1
7 1 2 6 12 13 3 2

Output:
1
0

Added by:mohamedwahba
Date:2011-12-15
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64
Resource:Arab Collegiate Programming Contest 2011

hide comments
2015-08-26 14:26:45 thirupathireddy
O(n*m) passing is not good............
2015-08-04 13:53:24 Indian Cyber Army (IndiShell)
Please move this to tutorials.
2015-06-02 11:09:21 Arjun Verma
solved it using brute force .
But could somebody help me out with the binary search approach
here is my code : <snip>
logic : sorted the second array - say B and then for every element in the first array (say A) , i found the minimum difference and then the overall minimum value .

Last edit: 2023-07-24 21:35:27
2015-04-18 08:27:24 :.Mohib.:
cpp rockss...
2015-04-16 15:04:32 Vicky
simple brute force ...;)
2015-04-02 10:41:35 xxbloodysantaxx
Dis it in O((n1+n2)log(n2))
2015-03-01 19:30:50 Abhinandan Agarwal
O(nXm) is accepted, it can become a good problem with more constricted time lmit
2015-01-28 11:31:54 Saurabh Goyal
Did it in o((n+m)log(n+m))
2014-02-12 15:33:51 mrolympia
O(n*m) will pass, but can also be done in O(n*log(m) + m*log(n)) with less time
2013-05-21 07:33:52 Seshadri R
Any altitude value will be between 1 and 1,000,000, inclusive. Just before the word inclusive, there is a non-conclusive comma (,). Are there more zeroes to follow this?
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.