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.*;
3+ import java.util.*;
4+
5+ public class Main {
6+ public static void main(String[] args) throws IOException {
7+ BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
8+
9+ int n = Integer.parseInt(br.readLine());
10+ int[] arr = new int[n];
11+
12+ StringTokenizer st = new StringTokenizer(br.readLine());
13+ for (int i = 0; i < n; i++) {
14+ arr[i] = Integer.parseInt(st.nextToken());
15+ }
16+
17+ int left = 0;
18+ int right = n - 1;
19+ int minSum = Integer.MAX_VALUE;
20+
21+ while (left < right) {
22+ int sum = arr[left] + arr[right];
23+
24+ if (Math.abs(sum) < Math.abs(minSum)) {
25+ minSum = sum;
26+ }
27+
28+ if (sum < 0) {
29+ left++;
30+ } else if (sum > 0) {
31+ right--;
32+ } else {
33+ break;
34+ }
35+ }
36+
37+ System.out.println(minSum);
38+ }
39+ }
40+ ``
You can’t perform that action at this time.
0 commit comments