Skip to content

Commit 2e55108

Browse files
authored
Merge pull request #552 from AlgorithmWithGod/LiiNi-coder
[20250726] BOJ / G5 / 두 용액 / 이인희
2 parents 6a378e6 + d285d57 commit 2e55108

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
```java
2+
import java.io.BufferedReader;
3+
import java.io.InputStreamReader;
4+
import java.util.Arrays;
5+
import java.util.StringTokenizer;
6+
7+
public class Main {
8+
9+
private static BufferedReader br;
10+
private static int n;
11+
private static int[] arr;
12+
13+
public static void main(String[] args) throws Exception {
14+
br = new BufferedReader(new InputStreamReader(System.in));
15+
16+
n = Integer.parseInt(br.readLine());
17+
arr = new int[n];
18+
19+
StringTokenizer st = new StringTokenizer(br.readLine());
20+
for (int i = 0; i < n; i++) {
21+
arr[i] = Integer.parseInt(st.nextToken());
22+
}
23+
24+
Arrays.sort(arr);
25+
26+
int left = 0;
27+
int right = n - 1;
28+
29+
int ansL = arr[left];
30+
int ansR = arr[right];
31+
int minGap = Math.abs(ansL + ansR);
32+
while (left < right) {
33+
int sum = arr[left] + arr[right];
34+
if (Math.abs(sum) < minGap) {
35+
minGap = Math.abs(sum);
36+
ansL = arr[left];
37+
ansR = arr[right];
38+
}
39+
if (sum < 0) left++;
40+
else right--;
41+
}
42+
43+
System.out.println(ansL + " " + ansR);
44+
}
45+
}
46+
```

0 commit comments

Comments
 (0)