ํฌํฌ์ธํฐ ๋ฅผ ์ด์ฉํด์
๋ฌธ์ ๋ฅผ ํ์ด ๋ณด์์ต๋๋ค.
import java.util.Arrays;
import java.util.Scanner;
public class N3273 {
public static void main(String[] args) {
N3273 T = new N3273();
Scanner kb = new Scanner(System.in);
int n = kb.nextInt();
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = kb.nextInt();
}
int x = kb.nextInt();
System.out.println(T.solution(n, arr, x));
}
public int solution(int n, int[] arr, int x) {
int answer = 0;
Arrays.sort(arr);
int lt = 0, rt = n - 1;
while (lt < rt) {
int sum = arr[lt] + arr[rt];
if (sum == x) {
answer++;
lt++;
rt--;
} else if (sum > x) {
rt--;
}else {
lt++;
}
}
return answer;
}
}
'๐ป ๊ฐ๋ฐ๊ณต๋ถ > Algorithm' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Eric's ๋ฐฑ์ค] 14425๋ฒ - ๋ฌธ์์ด ์งํฉ - Java (0) | 2023.02.01 |
---|---|
[Eric's ๋ฐฑ์ค] 10809๋ฒ - ์ํ๋ฒณ์ฐพ๊ธฐ - Java (0) | 2023.01.26 |
[Eric's ๋ฐฑ์ค] 1712๋ฒ - ์์ต๋ถ๊ธฐ์ - Java (0) | 2023.01.17 |
[Eric's ๋ฐฑ์ค] 1806๋ฒ - ๋ถ๋ถํฉ - Java (0) | 2023.01.16 |
[Eric's ๋ฐฑ์ค] 27160๋ฒ - ํ ๋ฆฌ๊ฐ๋ฆฌ - Java - 2023๋ณด๋๊ฒ์์ปต (0) | 2023.01.15 |