-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDay 155.java
More file actions
39 lines (29 loc) · 938 Bytes
/
Day 155.java
File metadata and controls
39 lines (29 loc) · 938 Bytes
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
import java.util.*;
class Solution {
public static ArrayList<Integer> findClosestPair(int arr1[], int arr2[], int x) {
int n = arr1.length;
int m = arr2.length;
int left = 0;
int right = m - 1;
int minDiff = Integer.MAX_VALUE;
int res1 = 0, res2 = 0;
while (left < n && right >= 0) {
int sum = arr1[left] + arr2[right];
int diff = Math.abs(sum - x);
if (diff < minDiff) {
minDiff = diff;
res1 = arr1[left];
res2 = arr2[right];
}
if (sum > x) {
right--;
} else {
left++;
}
}
ArrayList<Integer> result = new ArrayList<>();
result.add(res1);
result.add(res2);
return result;
}
}