File tree Expand file tree Collapse file tree 1 file changed +50
-0
lines changed
Expand file tree Collapse file tree 1 file changed +50
-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+ static int N ;
7+ static long [] arr, answer;
8+ static long minAbs;
9+
10+ public static void main (String [] args ) throws IOException {
11+ BufferedReader br = new BufferedReader (new InputStreamReader (System . in));
12+ N = Integer . parseInt(br. readLine());
13+ arr = new long [N ];
14+ StringTokenizer st = new StringTokenizer (br. readLine());
15+ for (int i = 0 ; i < N ; i++ ) {
16+ arr[i] = Long . parseLong(st. nextToken());
17+ }
18+ Arrays . sort(arr);
19+ minAbs = 3_000_000_000L ;
20+ for (int startIdx = 0 ; startIdx < N - 2 ; startIdx++ ) {
21+ twoPointer(startIdx);
22+ }
23+ for (long num : answer) {
24+ System . out. print(num + " " );
25+ }
26+ br. close();
27+ }
28+
29+ private static void twoPointer (int startIdx ) {
30+ int leftIdx = startIdx + 1 ;
31+ int rightIdx = N - 1 ;
32+ while (leftIdx < rightIdx) {
33+ long currentVal = arr[startIdx] + arr[leftIdx] + arr[rightIdx];
34+ long currentAbs = Math . abs(currentVal);
35+ if (currentAbs < minAbs) {
36+ minAbs = currentAbs;
37+ answer = new long [] {arr[startIdx], arr[leftIdx], arr[rightIdx]};
38+ }
39+ if (currentVal < 0 ) {
40+ leftIdx++ ;
41+ } else if (currentVal > 0 ) {
42+ rightIdx-- ;
43+ } else {
44+ break ;
45+ }
46+ }
47+ }
48+ }
49+
50+ ```
You can’t perform that action at this time.
0 commit comments