-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclosest_pair_from_two_arrays.java
90 lines (77 loc) · 1.86 KB
/
closest_pair_from_two_arrays.java
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
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
*/
/*package whatever //do not write package name here */
import java.util.*;
import java.lang.*;
import java.io.*;
class GFG {
public static void main (String[] args) {
//code
Scanner sc= new Scanner(System.in);
int t= sc.nextInt();
while(t-->0)
{
int m= sc.nextInt();
int n= sc.nextInt();
int a[]= new int[m];
int b[]= new int[n];
int ab[]= new int[m+n];
// System.out.println("a");
for(int i=0;i<m;i++)
{
a[i]=sc.nextInt();
}
// System.out.println("b");
for(int i=0;i<n;i++)
{
b[i]=sc.nextInt();
}
int sum=sc.nextInt();
Arrays.sort(a);
Arrays.sort(b);
int res_l=0,res_r=0;
int l=0,r=n-1;
int diff= 1000000;
while(l<m && r>=0)
{
if(Math.abs((a[l]+b[r])-sum)<diff)
{
res_l=l;
res_r=r;
diff=Math.abs((a[l]+b[r])-sum);
// System.out.println("diff "+diff);
}
// If sum of this pair is more than x, move to smaller
// side
if (a[l] + b[r] > sum)
r--;
else // move to the greater side
l++;
}
System.out.println(a[res_l]+" "+b[res_r]);
}
}
}