Skip to content

Commit 73b3aca

Browse files
committed
[BOJ] 두 용액 / 골드 5 / 40분
https://www.acmicpc.net/problem/2470
1 parent 0c2503c commit 73b3aca

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import sys
2+
3+
N = int(sys.stdin.readline())
4+
arr = sorted(map(int, sys.stdin.readline().split()))
5+
6+
left = 0
7+
right = N - 1
8+
9+
answer = (arr[left], arr[right], abs(arr[left] + arr[right]))
10+
11+
# 투 포인터 사용해서 절대값이 0에 가장 가까운 두 값 찾기
12+
while left < right:
13+
total = arr[left] + arr[right]
14+
abs_total = abs(total)
15+
16+
if abs_total < answer[2]:
17+
answer = (arr[left], arr[right], abs_total)
18+
if abs_total == 0:
19+
break
20+
# 두 용액의 값이 0보다 크면 right를 줄여서 합을 작게 만든다
21+
if total > 0:
22+
right -= 1
23+
else:
24+
# 두 용액의 값이 0보다 작으면 left를 늘려서 합을 크게 만든다
25+
left += 1
26+
27+
print(f"{answer[0]} {answer[1]}")

0 commit comments

Comments
 (0)