알고리즘/알고리즘(초급)
투포인터
상상2
2022. 11. 23. 22:58
https://www.acmicpc.net/problem/3273
3273번: 두 수의 합
n개의 서로 다른 양의 정수 a1, a2, ..., an으로 이루어진 수열이 있다. ai의 값은 1보다 크거나 같고, 1000000보다 작거나 같은 자연수이다. 자연수 x가 주어졌을 때, ai + aj = x (1 ≤ i < j ≤ n)을 만족하는
www.acmicpc.net
import sys
input = sys.stdin.readline
N = int(input())
arr_list = list(map(int, input().split()))
X = int(input())
arr_list.sort()
left, right = 0, N -1
result = 0
while left < right:
tmp = arr_list[left] + arr_list[right]
if tmp == X:
result += 1
if tmp < X:
left +=1
else:
right -=1
print(result)