`
효율적인 풀이를 계속 생각했으나 input size가 작은 편이고 시간도 2초나 주어져서 통과할 수 있었음.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
public class B20366 {
static int N;
static int arr[];
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
N = Integer.parseInt(br.readLine());
StringTokenizer st = new StringTokenizer(br.readLine());
arr = new int[N];
for (int i = 0; i < N; i++) {
arr[i] = Integer.parseInt(st.nextToken());
}
Arrays.sort(arr);
int ans = Integer.MAX_VALUE;
for (int i = 0; i < N - 3; i++){
for (int j = i+3; j < N; j++){
int elja = arr[i] + arr[j];
int left = i + 1;
int right = j - 1;
while (left < right){
int anna = arr[left] + arr[right];
if (anna < elja) left++; // 합 늘리기
else right--; // 합 줄이기
ans = Math.min(ans, Math.abs(elja - anna));
}
}
}
System.out.println(ans);
}
}
|