The first line gives the count n and the target t, and the second line gives n integers separated by spaces.
The distance between a number x and t is |x - t|.
Find the number among the n whose distance to t is the smallest.
If several numbers share the smallest distance, choose the smallest of them (with t = 0, given 5 and -5, choose -5).
Print the chosen number and its distance on one line, separated by one space.
Input
1 ≤ n ≤ 10,000, -1,500,000,000 ≤ t ≤ 1,500,000,000, -1,500,000,000 ≤ each integer ≤ 1,500,000,000
Output
The chosen number and its distance on one line
Example 1 · stdin
4 10
12 8 15 5
stdout
8 2
Example 2 · stdin
2 1500000000
-1500000000 1499999999
stdout
1499999999 1
main.py
Error List00F8 ↓ · ⇧F8 ↑
Input (stdin)
When you run, these lines are fed to input()/stdin in order (the program doesn't stop to ask).
$ Ready
Open this on a computer to write and run code. On mobile you can read the problem and the solutions.
In other languagesShow solutions by language · 8 languages
The absolute value function is Python abs(), JS/TS Math.abs, Java Math.abs, C# Math.Abs, C++ std::abs and Craykon Mathf.Abs. C has one per type: abs for int, llabs for long long. The first trap is overflow. The numbers and t fit in an int, but the difference x - t can reach 3 billion, beyond an int (up to about 2.147 billion). Subtracting two ints in C, C++, Java or C# overflows. In Java and C# the value wraps around to a negative number (in C and C++ it is undefined behavior), and Math.abs / Math.Abs cannot repair a difference that has already overflowed, so the wrong number looks the closest. Read the values as long (long long in C/C++), or convert before subtracting. In C, abs on a long long difference cuts it to int and causes the same problem, so use llabs. Python integers and the numbers of JS, TS and Craykon (doubles) do not overflow in this range. The second trap is ties. Asking only d < bestD keeps the number that came first, so for 12 8 you pick 12; also ask d == bestD && x < best. Comparing x - t without the absolute value makes a number far below t look "the closest".
Pythonmain.py · 8 lines
n, t = map(int, input().split())best = Nonefor x in map(int, input().split()): d = abs(x - t) # 더 가깝거나, 같은 거리인데 더 작은 수면 바꾼다 if best is None or d < best_d or (d == best_d and x < best): best, best_d = x, dprint(best, best_d)
JavaScriptmain.js · 11 lines
const [n, t, ...a] = require("fs").readFileSync(0, "utf8").trim().split(/\s+/).map(Number);let best = a[0];let bestD = Math.abs(a[0] - t);for (const x of a) { const d = Math.abs(x - t); if (d < bestD || (d === bestD && x < best)) { best = x; bestD = d; }}console.log(`${best} ${bestD}`);
TypeScriptmain.ts · 12 lines
import { readFileSync } from "node:fs";const [n, t, ...a]: number[] = readFileSync(0, "utf8").trim().split(/\s+/).map(Number);let best: number = a[0];let bestD: number = Math.abs(a[0] - t);for (const x of a) { const d = Math.abs(x - t); if (d < bestD || (d === bestD && x < best)) { best = x; bestD = d; }}console.log(`${best} ${bestD}`);
JavaMain.java · 24 lines
import java.io.BufferedReader;import java.io.InputStreamReader;import java.util.StringTokenizer;public class Main { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(br.readLine()); int n = Integer.parseInt(st.nextToken()); long t = Long.parseLong(st.nextToken()); st = new StringTokenizer(br.readLine()); long best = 0, bestD = -1; for (int i = 0; i < n; i++) { long x = Long.parseLong(st.nextToken()); // long으로 빼야 30억 차이가 넘치지 않는다 long d = Math.abs(x - t); if (bestD < 0 || d < bestD || (d == bestD && x < best)) { best = x; bestD = d; } } System.out.println(best + " " + bestD); }}
C#Main.cs · 22 lines
using System;class Program{ static void Main() { string[] first = Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries); long t = long.Parse(first[1]); long best = 0, bestD = -1; foreach (string s in Console.ReadLine().Split(' ', StringSplitOptions.RemoveEmptyEntries)) { long x = long.Parse(s); long d = Math.Abs(x - t); // long끼리 빼야 넘치지 않는다 if (bestD < 0 || d < bestD || (d == bestD && x < best)) { best = x; bestD = d; } } Console.WriteLine($"{best} {bestD}"); }}
C++main.cpp · 19 lines
#include <cstdlib>#include <iostream>int main() { int n; long long t; std::cin >> n >> t; long long best = 0, bestD = -1; for (int i = 0; i < n; i++) { long long x; std::cin >> x; long long d = std::abs(x - t); // long long 오버로드 if (bestD < 0 || d < bestD || (d == bestD && x < best)) { best = x; bestD = d; } } std::cout << best << " " << bestD << "\n";}
Cmain.c · 19 lines
#include <stdio.h>#include <stdlib.h>int main(void) { int n; long long t, x, best = 0, bestD = -1; scanf("%d %lld", &n, &t); for (int i = 0; i < n; i++) { scanf("%lld", &x); /* long long에는 abs가 아니라 llabs */ long long d = llabs(x - t); if (bestD < 0 || d < bestD || (d == bestD && x < best)) { best = x; bestD = d; } } printf("%lld %lld\n", best, bestD); return 0;}
Craykonmain.cray · 16 lines
p = Console.ReadLine().Trim().Split(" ");t = p[1].ToNumber();a = Console.ReadNumbers();best = a[0];bestD = Mathf.Abs(a[0] - t);foreach (x in a){ d = Mathf.Abs(x - t); // 더 가깝거나, 같은 거리인데 더 작은 수면 바꾼다 if (d < bestD || d == bestD && x < best) { best = x; bestD = d; }}Console.WriteLine(best + " " + bestD);