Skip to content

Commit 7181694

Browse files
committed
[Silver III] Title: 두 수의 합, Time: 180 ms, Memory: 18672 KB -BaekjoonHub
1 parent 6b1edd0 commit 7181694

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# [Silver III] 두 수의 합 - 3273
2+
3+
[문제 링크](https://www.acmicpc.net/problem/3273)
4+
5+
### 성능 요약
6+
7+
메모리: 18672 KB, 시간: 180 ms
8+
9+
### 분류
10+
11+
정렬, 두 포인터
12+
13+
### 제출 일자
14+
15+
2026년 3월 3일 16:26:59
16+
17+
### 문제 설명
18+
19+
<p>n개의 서로 다른 양의 정수 a<sub>1</sub>, a<sub>2</sub>, ..., a<sub>n</sub>으로 이루어진 수열이 있다. a<sub>i</sub>의 값은 1보다 크거나 같고, 1000000보다 작거나 같은 자연수이다. 자연수 x가 주어졌을 때, a<sub>i</sub> + a<sub>j</sub> = x (1 ≤ i < j ≤ n)을 만족하는 (a<sub>i</sub>, a<sub>j</sub>)쌍의 수를 구하는 프로그램을 작성하시오.</p>
20+
21+
### 입력
22+
23+
<p>첫째 줄에 수열의 크기 n이 주어진다. 다음 줄에는 수열에 포함되는 수가 주어진다. 셋째 줄에는 x가 주어진다. (1 ≤ n ≤ 100000, 1 ≤ x ≤ 2000000)</p>
24+
25+
### 출력
26+
27+
<p>문제의 조건을 만족하는 쌍의 개수를 출력한다.</p>
28+
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
"use strict";
2+
3+
const fs = require("fs");
4+
5+
const input = fs
6+
.readFileSync(process.platform === "linux" ? "/dev/stdin" : "./input.txt")
7+
.toString()
8+
.trim()
9+
.split("\n");
10+
11+
const n = +input[0];
12+
const numbers = input[1]
13+
.split(" ")
14+
.map(Number)
15+
.sort((a, b) => a - b);
16+
const x = +input[2];
17+
18+
/**
19+
* a[i]+a[j] = x를 만족하는 (a[i],a[j])쌍의 수 = ?
20+
*/
21+
22+
let left = 0;
23+
let right = n - 1;
24+
let count = 0;
25+
26+
while (left < right) {
27+
const sum = numbers[left] + numbers[right];
28+
if (sum === x) {
29+
count++;
30+
left++;
31+
right--;
32+
} else if (sum < x) {
33+
left++;
34+
} else {
35+
right--;
36+
}
37+
}
38+
39+
console.log(count);

0 commit comments

Comments
 (0)