Skip to content

Commit fef080f

Browse files
committed
[BOJ] #3273. 두 수의 합 / 실버3 / 19분 / 성공
1 parent 10419ac commit fef080f

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import sys
2+
input = sys.stdin.readline
3+
4+
# 입력 받기
5+
n = int(input()) # 수열의 크기
6+
numbers = list(map(int, input().split())) # 수열
7+
x = int(input()) # 목표 숫자
8+
9+
# 초기화
10+
left = 0
11+
right = n - 1
12+
count = 0
13+
14+
# 수열 정렬
15+
numbers.sort()
16+
17+
# 투 포인터 탐색
18+
while left < right:
19+
current_sum = numbers[left] + numbers[right]
20+
if current_sum > x:
21+
right -= 1 # 합이 크면 오른쪽 포인터를 왼쪽으로
22+
elif current_sum < x:
23+
left += 1 # 합이 작으면 왼쪽 포인터를 오른쪽으로
24+
else:
25+
count += 1 # 조건 만족
26+
left += 1
27+
right -= 1
28+
29+
# 결과 출력
30+
print(count)

0 commit comments

Comments
 (0)