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