[알고리즘] 백준 - 두 수의 합

June·2021년 3월 8일
0

알고리즘

목록 보기
125/260

백준 - 두 수의 합

내 풀이

sys.setrecursionlimit(100000)

n = int(input())
arr = list(map(int, input().split()))
x = int(input())
arr.sort()

left, right = 0, len(arr)-1

count = 0
while left < right:
    if arr[left] + arr[right] == x:
        count += 1
        left += 1
    elif arr[left] + arr[right] < x:
        left += 1
    else:
        right -= 1

print(count)

이분 탐색이랑 다르게 조건이 left < right이다. left <= right가 아니고.

0개의 댓글