투 포인터로 풀이한 문제로 start와 end를 언제 옮길지만 생각해내면 쉽게 풀이할 수 있는 문제다.
start는 0, end는 N-1로 초기값을 설정한 뒤, arr[start] + arr[end]의 절댓값이 0보다 크거나 같을 경우에는 end를 한 칸 앞으로, 0보다 작을 경우에는 start를 한 칸 뒤로 보내준다.
그리고 min은 arr[start] + arr[end]의 절댓값이 min보다 작을 경우에만 초기화 해준다.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
/**
* 백준 2467번 용액
* - 투 포인터
*/
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
StringTokenizer st = new StringTokenizer(br.readLine());
int arr[] = new int[N];
for (int i = 0; i < N; i++) {
arr[i] = Integer.parseInt(st.nextToken());
}
// 양 끝에서부터 비교
int start = 0;
int end = N - 1;
int startN = 0;
int endN = 0;
int min = Integer.MAX_VALUE;
while (start < end) {
if (min > Math.abs(arr[end] + arr[start])) {
min = Math.abs(arr[end] + arr[start]);
startN = arr[start];
endN = arr[end];
}
if (arr[end] + arr[start] > 0) end--;
else start++;
}
System.out.println(startN + " " + endN);
}
}