File tree Expand file tree Collapse file tree 1 file changed +46
-0
lines changed
Expand file tree Collapse file tree 1 file changed +46
-0
lines changed Original file line number Diff line number Diff line change 1+ ``` java
2+ import java.io.BufferedReader ;
3+ import java.io.InputStreamReader ;
4+ import java.util.Arrays ;
5+ import java.util.StringTokenizer ;
6+
7+ public class Main {
8+
9+ private static BufferedReader br;
10+ private static int n;
11+ private static int [] arr;
12+
13+ public static void main (String [] args ) throws Exception {
14+ br = new BufferedReader (new InputStreamReader (System . in));
15+
16+ n = Integer . parseInt(br. readLine());
17+ arr = new int [n];
18+
19+ StringTokenizer st = new StringTokenizer (br. readLine());
20+ for (int i = 0 ; i < n; i++ ) {
21+ arr[i] = Integer . parseInt(st. nextToken());
22+ }
23+
24+ Arrays . sort(arr);
25+
26+ int left = 0 ;
27+ int right = n - 1 ;
28+
29+ int ansL = arr[left];
30+ int ansR = arr[right];
31+ int minGap = Math . abs(ansL + ansR);
32+ while (left < right) {
33+ int sum = arr[left] + arr[right];
34+ if (Math . abs(sum) < minGap) {
35+ minGap = Math . abs(sum);
36+ ansL = arr[left];
37+ ansR = arr[right];
38+ }
39+ if (sum < 0 ) left++ ;
40+ else right-- ;
41+ }
42+
43+ System . out. println(ansL + " " + ansR);
44+ }
45+ }
46+ ```
You can’t perform that action at this time.
0 commit comments