[백준 3273번 C/C++] 두 수의 합
https://www.acmicpc.net/problem/3273
해결방안
투 포인터 알고리즘
코드
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int n, x;
vector<int> a;
int answer;
int main(){
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> n;
a.resize(n);
for(int i = 0; i< n; i++){
cin >> a[i];
}
cin >> x;
sort(a.begin(), a.end()); // 1 2 3 5 7 9 10 11 12 정렬
int left = 0, right = n - 1;
while (left < right)
{
if (a[left] + a[right] == x){
answer++;
left++;
}
else if (a[left] + a[right] < x){
left++;
}
else if (a[left] + a[right] > x){
right--;
}
}
cout << answer;
return 0;
}