-
Notifications
You must be signed in to change notification settings - Fork 22
/
find the closest pair from two arrays
67 lines (55 loc) · 1.41 KB
/
find the closest pair from two arrays
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
/*
Given two arrays and a number x, find the pair whose sum is closest to x and the pair has an element from each array.
Input:
The first line consists of a single integer T, the number of test cases. For each test case, the first line contains 2 integers n & m
denoting the size of two arrays. Next line contains n- space separated integers denoting the elements of array A and next lines contains m
space separated integers denoting the elements of array B followed by an integer x denoting the number whose closest sum is to find.
Output:
For each test case, the output is 2 space separated integers whose sum is closest to x.
Constraints:
1<=T<=100
1<=n,m<=50
1<=A[i],B[i]<=500
Example:
Input:
2
4 4
1 4 5 7
10 20 30 40
32
4 4
1 4 5 7
10 20 30 40
50
Output:
1 30
7 40
*/
#include <bits/stdc++.h>
using namespace std;
int main() {
int t; cin>>t; while(t--){
int m,n,x; cin>>m>>n;
int a[m],b[n];
for(int i=0; i<m; i++) cin>>a[i];
for(int j=0; j<n; j++) cin>>b[j];
cin>>x;
sort(a,a+m);
sort(b,b+n);
int i=0,j=n-1,p,q,diff=INT_MAX;
while(i<m && j>=0){
if (abs(a[i] + b[j] - x) < diff)
{
p = i;
q = j;
diff = abs(a[i] + b[j] - x);
}
if (a[i] + b[j] > x)
j--;
else
i++;
}
cout<<a[p]<<" "<<b[q]<<'\n';
}
return 0;
}