#C243. Find K Closest Elements
Find K Closest Elements
Find K Closest Elements
Given an array \(A\) of \(n\) integers, an integer \(k\), and a target integer \(x\), your task is to find the \(k\) closest integers to \(x\) in the array. The closeness is determined by the absolute difference \(|A_i - x|\). In case of ties, the smaller integer is considered closer. The final answer should be sorted in ascending order.
Note: You must read from standard input (stdin) and output your result to standard output (stdout).
Example: For input
5
1 2 3 4 5
4
3
the output should be
1 2 3 4
inputFormat
The input is read from standard input and consists of four lines:
- The first line contains an integer \(n\), representing the number of elements in the array.
- The second line contains \(n\) space-separated integers denoting the elements of the array \(A\).
- The third line contains an integer \(k\), the number of closest elements to find.
- The fourth line contains an integer \(x\), the target value.
outputFormat
Output the \(k\) closest integers to \(x\) in ascending order, separated by a single space.
## sample5
1 2 3 4 5
4
3
1 2 3 4