Skip to content

Commit fd4a26a

Browse files
committed
[Silver III] Title: 두 수의 합, Time: 308 ms, Memory: 27060 KB -BaekjoonHub
1 parent facb75d commit fd4a26a

2 files changed

Lines changed: 49 additions & 1 deletion

File tree

백준/Silver/3273. 두 수의 합/README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,16 @@
44

55
### 성능 요약
66

7-
메모리: 126016 KB, 시간: 156 ms
7+
메모리: 27060 KB, 시간: 308 ms
88

99
### 분류
1010

1111
정렬, 두 포인터
1212

13+
### 제출 일자
14+
15+
2026년 1월 14일 21:47:18
16+
1317
### 문제 설명
1418

1519
<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>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import java.io.BufferedReader;
2+
//import java.io.FileInputStream;
3+
import java.io.InputStreamReader;
4+
import java.util.*;
5+
6+
public class Main {
7+
8+
private static int target;
9+
private static int result;
10+
private static int[] nums;
11+
12+
public static void main(String[] args) throws Exception {
13+
// System.setIn(new FileInputStream("input.txt")); // 제출 시 이 줄만 주석처리
14+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
15+
16+
int n = Integer.parseInt(br.readLine());
17+
nums = new int[n];
18+
19+
StringTokenizer st = new StringTokenizer(br.readLine());
20+
for (int i = 0; i < n; i++) {
21+
nums[i] = Integer.parseInt(st.nextToken());
22+
}
23+
target = Integer.parseInt(br.readLine());
24+
25+
Arrays.sort(nums);
26+
27+
int left = 0;
28+
int right = n - 1;
29+
30+
while (left < right) {
31+
int sum = nums[left] + nums[right];
32+
if (sum == target) {
33+
result++;
34+
left++;
35+
} else if (sum < target) {
36+
left++;
37+
} else {
38+
right--;
39+
}
40+
}
41+
System.out.println(result);
42+
}
43+
44+
}

0 commit comments

Comments
 (0)