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