Skip to content

Commit 6677a06

Browse files
authored
Merge pull request #1034 from AlgorithmWithGod/LiiNi-coder
[20251003] BOJ / G5 / 블랙 프라이데이 / 이인희
2 parents 56923b8 + 265a2fe commit 6677a06

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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+
StringTokenizer st = new StringTokenizer(br.readLine());
9+
int N = Integer.parseInt(st.nextToken());
10+
int C = Integer.parseInt(st.nextToken());
11+
int[] arr = new int[N];
12+
st = new StringTokenizer(br.readLine());
13+
for (int i = 0; i < N; i++) {
14+
arr[i] = Integer.parseInt(st.nextToken());
15+
}
16+
17+
Arrays.sort(arr);
18+
19+
//1개 확인
20+
for(int i = 0; i < N; i++){
21+
if (arr[i] == C) {
22+
System.out.println(1);
23+
return;
24+
}
25+
}
26+
27+
//2개 확인(투포인터 사용)
28+
int l = 0, r = N - 1;
29+
while(l < r){
30+
int sum = arr[l] + arr[r];
31+
if(sum == C){
32+
System.out.println(1);
33+
return;
34+
}
35+
if(sum < C)
36+
l++;
37+
else
38+
r--;
39+
}
40+
41+
//3개 확인(중간값 정하고 나머지값 투포인터로 찾기)
42+
for(int i = 0; i < N; i++){
43+
int target = C - arr[i];
44+
int left = 0, right = N - 1;
45+
while(left < right){
46+
if(left == i){
47+
left++;
48+
continue;
49+
}
50+
if(right == i){
51+
right--;
52+
continue;
53+
}
54+
int sum = arr[left] + arr[right];
55+
if(sum == target){
56+
System.out.println(1);
57+
return;
58+
}
59+
if(sum < target)
60+
left++;
61+
else
62+
right--;
63+
}
64+
}
65+
66+
System.out.println(0);
67+
}
68+
}
69+
70+
```

0 commit comments

Comments
 (0)