Skip to content

Commit 840b78b

Browse files
authored
Merge pull request #1834 from AlgorithmWithGod/zinnnn37
[20260125] BOJ / G4 / 좋다 / 김민진
2 parents e49eccd + b14ccae commit 840b78b

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
```java
2+
import java.io.BufferedReader;
3+
import java.io.IOException;
4+
import java.io.InputStreamReader;
5+
import java.util.Arrays;
6+
import java.util.StringTokenizer;
7+
8+
public class BJ_1253_좋다 {
9+
10+
private static final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
11+
private static StringTokenizer st;
12+
13+
private static int N, cnt;
14+
private static long[] arr;
15+
16+
public static void main(String[] args) throws IOException {
17+
init();
18+
sol();
19+
}
20+
21+
private static void init() throws IOException {
22+
N = Integer.parseInt(br.readLine());
23+
cnt = 0;
24+
25+
arr = new long[N];
26+
st = new StringTokenizer(br.readLine());
27+
for (int i = 0; i < N; i++) {
28+
arr[i] = Long.parseLong(st.nextToken());
29+
}
30+
Arrays.sort(arr);
31+
}
32+
33+
private static void sol() {
34+
for (int i = 0; i < N; i++) {
35+
if (isGood(i)) {
36+
cnt++;
37+
}
38+
}
39+
System.out.println(cnt);
40+
}
41+
42+
private static boolean isGood(int idx) {
43+
long target = arr[idx];
44+
int left = 0;
45+
int right = N - 1;
46+
47+
while (left < right) {
48+
if (left == idx) {
49+
left++;
50+
continue;
51+
} else if (right == idx) {
52+
right--;
53+
continue;
54+
}
55+
56+
long current = arr[left] + arr[right];
57+
58+
if (current == target) {
59+
return true;
60+
} else if (current < target) {
61+
left++;
62+
} else {
63+
right--;
64+
}
65+
}
66+
67+
return false;
68+
}
69+
70+
}
71+
```

0 commit comments

Comments
 (0)