File tree Expand file tree Collapse file tree 1 file changed +77
-0
lines changed
Expand file tree Collapse file tree 1 file changed +77
-0
lines changed Original file line number Diff line number Diff line change 1+ ```
2+ import java.io.*;
3+ import java.util.*;
4+
5+ public class Main {
6+ private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
7+ private static final BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
8+ private static int[] arr, answer;
9+ private static int N, diff;
10+ public static void main(String[] args) throws IOException {
11+ init();
12+ solve();
13+
14+ bw.write(answer[0] + " " + answer[1] + "\n");
15+ bw.flush();
16+ bw.close();
17+ br.close();
18+ }
19+
20+ private static void init() throws IOException {
21+ N = Integer.parseInt(br.readLine());
22+ arr = new int[N];
23+ answer = new int[2];
24+ diff = 2000000001;
25+
26+ StringTokenizer st = new StringTokenizer(br.readLine());
27+
28+ for (int i = 0; i < N; i++) {
29+ arr[i] = Integer.parseInt(st.nextToken());
30+ }
31+
32+ Arrays.sort(arr);
33+ }
34+
35+ private static void solve() {
36+ for (int i = 0; i < N-1; i++) {
37+ binarySearch(i);
38+ }
39+ }
40+
41+ private static void binarySearch(int start) {
42+ int left = start+1;
43+ int right = N-1;
44+
45+ while (left <= right) {
46+ int mid = left + (right-left)/2;
47+ int temp = arr[start]+arr[mid];
48+
49+
50+ if (temp < 0) {
51+ if (refresh(temp)) {
52+ answer[0] = arr[start];
53+ answer[1] = arr[mid];
54+ diff = Math.abs(temp);
55+ }
56+ left = mid+1;
57+ } else if (temp == 0) {
58+ answer[0] = arr[start];
59+ answer[1] = arr[mid];
60+ diff = 0;
61+ return;
62+ } else {
63+ if (refresh(temp)) {
64+ answer[0] = arr[start];
65+ answer[1] = arr[mid];
66+ diff = Math.abs(temp);
67+ }
68+ right = mid-1;
69+ }
70+ }
71+ }
72+
73+ private static boolean refresh(int num) {
74+ return Math.abs(num) < diff;
75+ }
76+ }
77+ ```
You can’t perform that action at this time.
0 commit comments