[백준] 3273번: 두 수의 합

whitehousechef·2023년 10월 12일
0

https://www.acmicpc.net/problem/3273

initial

this is really simple. We first sort the list in ascending order and if sum>x, we decerement right pointer since it will decrease our value of sum and else, increment left pointer.

solution

import sys
input = sys.stdin.readline

n=int(input())
lst = list(map(int,input().split()))
m= int(input())
lst.sort()
left,right = 0, len(lst)-1
ans =0
while left<right:
    sum= lst[left]+lst[right]
    if sum==m:
        ans +=1
        left+=1
        right-=1
    elif sum>m:
        right-=1
    else:
        left+=1
print(ans)

complexity

n time and space

0개의 댓글