Skip to content

Commit

Permalink
[solved] boj2467
Browse files Browse the repository at this point in the history
  • Loading branch information
olrlobt committed Oct 26, 2023
1 parent 174f57e commit bb83abc
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions LeeSeungheon/boj/boj2467.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;

public class boj2467 {

public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringBuilder sb = new StringBuilder();

int N = Integer.parseInt(br.readLine());
int[] map = new int[N];

StringTokenizer st = new StringTokenizer(br.readLine());
for (int num = 0; num < N; num++) {
map[num] = Integer.parseInt(st.nextToken());
}
int[] solve = solve(map);

sb.append(map[solve[0]]).append(" ").append(map[solve[1]]);
System.out.println(sb);
}

private static int[] solve(int[] map) {

Arrays.sort(map);
int sum = Integer.MAX_VALUE;
int left = 0;
int right = map.length - 1;
int temp = 0;
int[] result = new int[2];

while (left < right) {

temp = map[left] + map[right];

if (Math.abs(temp) < sum) {
sum = Math.abs(temp);
result[0] = left;
result[1] = right;
}

if (temp < 0) {
left++;
} else {
right--;
}
}
return result;
}
}

0 comments on commit bb83abc

Please sign in to comment.