Skip to content

Commit 50ca99c

Browse files
authored
Merge pull request #1381 from AlgorithmWithGod/Seol-JY
[20251111] BOJ / G4 / 좋다 / 설진영
2 parents 37bab41 + 1d9b7de commit 50ca99c

File tree

1 file changed

+58
-0
lines changed

1 file changed

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

0 commit comments

Comments
 (0)