




두 방법으로 풀 수 있고
투 포인터를 사용하는 것이 시간 복잡도가 빠르지만
이번에는 이분 탐색 학습을 위해 1번 방식으로 문제를 풀었습니다.
투 포인터이던 이분 탐색이던 알고리즘을 사용하려면 정렬해야 합니다.
가장 먼저 해야할 것은 solutions배열의 정렬입니다.

두 용액을 고정하고 하나의 용액이 0에 가장 가깝도록
이분 탐색을 수행합니다.

twoSolutions 변수에 더해줍니다.

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;
public class Main {
static StringTokenizer st;
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
st = new StringTokenizer(br.readLine());
long[] solutions = new long[N];
for (int i = 0; i < N; i++) {
solutions[i] = Integer.parseInt(st.nextToken());
}
Arrays.sort(solutions);
sol(N, solutions);
}
static void sol(int N, long[] solutions) {
long min = Long.MAX_VALUE;
long[] answer = new long[3];
for (int i = 0; i < N-2; i++) {
for (int j = i + 1; j < N-1; j++) {
long twoSolutions = solutions[i] + solutions[j];
int s = j + 1;
int e = N - 1;
while (s <= e) {
int m = (s + e) / 2;
long checkMin = Math.abs(twoSolutions + solutions[m]);
if (twoSolutions + solutions[m] == 0) {
System.out.println(solutions[i] + " " + solutions[j] + " " + solutions[m]);
return;
}
if (checkMin < min) {
min = checkMin;
answer[0] = solutions[i];
answer[1] = solutions[j];
answer[2] = solutions[m];
}
if (twoSolutions + solutions[m] > 0) e = m - 1;
else s = m + 1;
}
}
}
System.out.println(answer[0] + " " + answer[1] + " " + answer[2]);
}
}