백준 2467번
https://www.acmicpc.net/problem/2467
import java.io.*;
import java.util.StringTokenizer;
public class Main {
// input
private static BufferedReader br;
// variables
private static int N;
private static int[] arr;
public static void main(String[] args) throws IOException {
br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
input();
bw.write(solve());
bw.close();
} // End of main()
private static String solve() {
StringBuilder sb = new StringBuilder();
int[] ret = twoPointer();
sb.append(ret[0]).append(' ').append(ret[1]);
return sb.toString();
} // End of solve()
private static int[] twoPointer() {
int low = 0;
int high = N - 1;
int min = Integer.MAX_VALUE;
int[] ret = new int[2];
while (low < high) {
int sum = arr[low] + arr[high];
int abs = Math.abs(sum);
if (abs < min) {
min = abs;
ret[0] = arr[low];
ret[1] = arr[high];
}
if (sum > 0) {
high--;
} else {
low++;
}
}
return ret;
} // End of twoPointer()
private static void input() throws IOException {
N = Integer.parseInt(br.readLine());
arr = new int[N + 1];
StringTokenizer st = new StringTokenizer(br.readLine());
for (int i = 0; i < N; i++) {
arr[i] = Integer.parseInt(st.nextToken());
}
} // End of input()
} // End of Main class
import java.io.*;
import java.util.StringTokenizer;
public class Main {
// input
private static BufferedReader br;
// variables
private static int N, min;
private static int[] arr, ret;
public static void main(String[] args) throws IOException {
br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
input();
bw.write(solve());
bw.close();
} // End of main()
private static String solve() {
StringBuilder sb = new StringBuilder();
twoPointer(0, N - 1);
sb.append(ret[0]).append(' ').append(ret[1]);
return sb.toString();
} // End of solve()
private static void twoPointer(int low, int high) {
if (low >= high) {
return;
}
int sum = arr[low] + arr[high];
int abs = Math.abs(arr[low] + arr[high]);
if (abs < min) {
min = abs;
ret[0] = arr[low];
ret[1] = arr[high];
}
if (sum == 0) return;
if (sum > 0) {
twoPointer(low, high - 1);
} else {
twoPointer(low + 1, high);
}
} // End of twoPointer()
private static void input() throws IOException {
N = Integer.parseInt(br.readLine());
arr = new int[N];
ret = new int[2];
min = Integer.MAX_VALUE;
StringTokenizer st = new StringTokenizer(br.readLine());
for (int i = 0; i < N; i++) {
arr[i] = Integer.parseInt(st.nextToken());
}
} // End of input()
} // End of Main class