Skip to content

Commit 929f23c

Browse files
authored
[20250713] BOJ / G3 / 세 용액 / 신동윤
1 parent 6dbb7c0 commit 929f23c

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
```java
2+
import java.io.*;
3+
import java.util.*;
4+
5+
public class Main {
6+
static int N;
7+
static long[] arr, answer;
8+
static long minAbs;
9+
10+
public static void main(String[] args) throws IOException {
11+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
12+
N = Integer.parseInt(br.readLine());
13+
arr = new long[N];
14+
StringTokenizer st = new StringTokenizer(br.readLine());
15+
for (int i = 0; i < N; i++) {
16+
arr[i] = Long.parseLong(st.nextToken());
17+
}
18+
Arrays.sort(arr);
19+
minAbs = 3_000_000_000L;
20+
for (int startIdx = 0; startIdx < N-2; startIdx++) {
21+
twoPointer(startIdx);
22+
}
23+
for (long num : answer) {
24+
System.out.print(num + " ");
25+
}
26+
br.close();
27+
}
28+
29+
private static void twoPointer(int startIdx) {
30+
int leftIdx = startIdx + 1;
31+
int rightIdx = N-1;
32+
while (leftIdx < rightIdx) {
33+
long currentVal = arr[startIdx] + arr[leftIdx] + arr[rightIdx];
34+
long currentAbs = Math.abs(currentVal);
35+
if (currentAbs < minAbs) {
36+
minAbs = currentAbs;
37+
answer = new long[] {arr[startIdx], arr[leftIdx], arr[rightIdx]};
38+
}
39+
if (currentVal < 0) {
40+
leftIdx++;
41+
} else if (currentVal > 0) {
42+
rightIdx--;
43+
} else {
44+
break;
45+
}
46+
}
47+
}
48+
}
49+
50+
```

0 commit comments

Comments
 (0)