From d2a2b6391461e93f7561aad12b42c8aa9eafcb16 Mon Sep 17 00:00:00 2001 From: suyeun84 <81475092+suyeun84@users.noreply.github.com> Date: Thu, 31 Jul 2025 22:38:13 +0900 Subject: [PATCH] =?UTF-8?q?Create=2031=20BOJ=20G5=20=EB=91=90=20=EC=88=98?= =?UTF-8?q?=EC=9D=98=20=ED=95=A9.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ... \354\210\230\354\235\230 \355\225\251.md" | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 "suyeun84/202507/31 BOJ G5 \353\221\220 \354\210\230\354\235\230 \355\225\251.md" diff --git "a/suyeun84/202507/31 BOJ G5 \353\221\220 \354\210\230\354\235\230 \355\225\251.md" "b/suyeun84/202507/31 BOJ G5 \353\221\220 \354\210\230\354\235\230 \355\225\251.md" new file mode 100644 index 00000000..796ad478 --- /dev/null +++ "b/suyeun84/202507/31 BOJ G5 \353\221\220 \354\210\230\354\235\230 \355\225\251.md" @@ -0,0 +1,55 @@ +```java +import java.io.*; +import java.util.*; + +class boj9024 { + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + static StringTokenizer st; + static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());} + static int nextInt() {return Integer.parseInt(st.nextToken());} + + public static void main(String[] args) throws Exception { + nextLine(); + int t = nextInt(); + while (t-- > 0) { + nextLine(); + int n = nextInt(); + int k = nextInt(); + + int[] arr = new int[n]; + nextLine(); + for (int i = 0; i < n; i++) { + arr[i] = nextInt(); + } + + Arrays.sort(arr); + int left = 0; + int right = arr.length - 1; + int best = (int)(2 * 10e8); + int count = 1; + + while (true) { + int sum = arr[left] + arr[right]; + + if (Math.abs(sum - k) == best) { + count++; + } else if (Math.abs(sum - k) < best) { + count = 1; + best = Math.abs(sum - k); + } + + if (sum == k) { + left++; + right--; + } else if (sum < k) left++; + else right--; + + if (left >= right) { + break; + } + } + System.out.println(count); + } + } +} +```