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