Problem ๐ป
Approach
- ๋ฆฌ์คํธ๊ฐ ์ ๋ ฌ๋์ด ์ฃผ์ด์ง๊ณ , ๋ ๊ฐ์ ํฉํด์ 0์ ๊ฐ๊น์ด ์๋ฅผ ์ฐพ์์ผํ๊ธฐ์, left์ right๋ฅผ ํ๋์ฉ ํ์ํด์ผ๊ฒ ๋ค ์๊ฐํด์ ํฌํฌ์ธํฐ ์๊ณ ๋ฆฌ์ฆ์ ์ฌ์ฉํ์๋ค.
- while๋ฌธ ์กฐ๊ฑด ๊ฐ ํญ์ ์ ํ์ธํ๊ธฐ!
- ๋ ์ฉ์ก์ ๊ฐ์ ๊ตฌํด์ผํ๊ธฐ ๋๋ฌธ์ left = right๊น์ง ๋๋ฆฌ๋ฉด ์๋๋ค!
- while left < right ๋ก ์กฐ๊ฑด์ ์ฃผ์ด์ผ ํจ
Solution ๐ก
import sys
N = int(sys.stdin.readline().strip())
lst = list(map(int, sys.stdin.readline().split()))
left = 0
right = len(lst) - 1
answer_left, answer_right = 0, 0
count = 1e99
while left < right:
check = lst[left] + lst[right]
if abs(check) < abs(count):
count = check
answer_left = lst[left]
answer_right = lst[right]
if check > 0:
right -= 1
elif check < 0:
left += 1
else:
break
print(answer_left, answer_right)