Skip to content

Commit cd43309

Browse files
authored
Merge pull request #584 from AlgorithmWithGod/suyeun84
[20250731] BOJ / G5 / 두 수의 합 / 김수연
2 parents beeaa3a + d2a2b63 commit cd43309

File tree

1 file changed

+55
-0
lines changed

1 file changed

+55
-0
lines changed
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
```java
2+
import java.io.*;
3+
import java.util.*;
4+
5+
class boj9024 {
6+
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
7+
static StringTokenizer st;
8+
static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());}
9+
static int nextInt() {return Integer.parseInt(st.nextToken());}
10+
11+
public static void main(String[] args) throws Exception {
12+
nextLine();
13+
int t = nextInt();
14+
while (t-- > 0) {
15+
nextLine();
16+
int n = nextInt();
17+
int k = nextInt();
18+
19+
int[] arr = new int[n];
20+
nextLine();
21+
for (int i = 0; i < n; i++) {
22+
arr[i] = nextInt();
23+
}
24+
25+
Arrays.sort(arr);
26+
int left = 0;
27+
int right = arr.length - 1;
28+
int best = (int)(2 * 10e8);
29+
int count = 1;
30+
31+
while (true) {
32+
int sum = arr[left] + arr[right];
33+
34+
if (Math.abs(sum - k) == best) {
35+
count++;
36+
} else if (Math.abs(sum - k) < best) {
37+
count = 1;
38+
best = Math.abs(sum - k);
39+
}
40+
41+
if (sum == k) {
42+
left++;
43+
right--;
44+
} else if (sum < k) left++;
45+
else right--;
46+
47+
if (left >= right) {
48+
break;
49+
}
50+
}
51+
System.out.println(count);
52+
}
53+
}
54+
}
55+
```

0 commit comments

Comments
 (0)