09009

[백준 3273번] 두 수의 합 본문

Algorithm/투 포인터
[백준 3273번] 두 수의 합
09009

문제 보기

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(map(int,input().split()))
x = int(input())

arr.sort()
start, end = 0, len(arr)-1
cnt = 0

while start < end:
    if arr[start] + arr[end] == x:
        end -= 1
        cnt += 1
    elif arr[start] + arr[end] > x:
        end -= 1
    elif arr[start] + arr[end] < x:
        start += 1


print(cnt)

'Algorithm > 투 포인터' 카테고리의 다른 글

[백준 1806번] 부분합  (0) 2023.09.08
[백준 2470번] 두 용액  (0) 2023.09.08