728x90
반응형
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

package BAEKJOON.Silver.Ⅲ;
import java.util.Arrays;
import java.util.Scanner;
public class NO3273 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int [] arr = new int [n];
for (int i = 0; i < n; i++) {
arr[i] = sc.nextInt();
}
int x = sc.nextInt();
Arrays.sort(arr);
int start = 0;
int end = n-1;
int count = 0;
while (start < end) {
if( arr[start] + arr[end] == x ) {
count ++;
start ++;
end --;
} else {
if ( arr[start] + arr[end] < x) {
start ++;
} else {
end --;
}
}
}
System.out.println(count);
}
}
728x90
반응형
'Algorithm & SQL > BAEKJOON' 카테고리의 다른 글
| 백준 JAVA 10815 숫자 카드 (0) | 2022.08.19 |
|---|---|
| 백준 JAVA 2003 수들의 합2 (0) | 2022.08.17 |
| 백준 JAVA 11728 배열 합치기 (0) | 2022.08.16 |
| 백준 JAVA 2018 수들의 합5 (0) | 2022.08.15 |
| 백준 JAVA 12845 모두의 마블 (0) | 2022.08.14 |
댓글