Closest Triplet
Easy Accuracy: 36.58% Submissions: 246 Points: 2

Given three sorted arrays A[], B[] and C[], your task is to write a program that helps to find 3 elements i, j and k from A, B and C respectively such that max(abs(A[i] – B[j]), abs(B[j] – C[k]), abs(C[k] – A[i])) is minimized. Here abs() indicates absolute value.

Example :

Input: A[] = {1, 4, 10}
       B[] = {2, 15, 20}
       C[] = {10, 12}
Output: 5
10 from A, 15 from B and 10 from C
max( abs(10-15) , abs(15-12) , abs(10-10)) that is: 5 
Input: A[] = {20, 24, 100}
       B[] = {2, 19, 22, 79, 800}
       C[] = {10, 12, 23, 24, 119}
Output: 2

Input:
First line of the input contains an integer T, denoting the number of test cases. Then T test case follows. First line of each test case contains an integer 'p' denoting the size of the first array A[]. Then next line contains 'p' space integers denoting the elemets of the array A[]. Next line contains an integer 'q'. Then next line contains 'q' space sperated integers denoting the elements of the array B[]. Next line contains an integer 'r' denoting the size of array C[]. The next line contains 'r' space separated integers denoting the elements of the array C[].


Output:
For each test case Print maximum abs difference of all pairs .

Constraints:
1<=T<=100
1<=p,q,r<=103

Example:
Input:

2
3
10 11 12
4
20 21 22 23
5
30 31 32 33 34
5
25 30 35 40 45
5
26 31 36 41 46
5
24 29 34 39 44
Output:
18
 
2

to report an issue on this page.

Editorial

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

Yes

All Submissions

My Submissions:

Login to access your submissions.