#C6288. Find the k Closest Elements
Find the k Closest Elements
Find the k Closest Elements
You are given a sorted array \(arr\) of \(n\) integers and two additional integers \(k\) and \(x\). Your task is to find the \(k\) closest integers to \(x\) from the array. The result should be sorted in ascending order.
If two numbers have the same absolute difference from \(x\), the smaller number is preferred. The input will be read from standard input and the output should be written to standard output.
For example, when \(arr = [1,2,3,4,5]\), \(k = 4\), and \(x = 3\), the correct output is 1 2 3 4.
The absolute difference is defined as \(|a - x|\), and during ties the smaller element is prioritized.
inputFormat
The first line of input contains three integers \(n\), \(k\), and \(x\) separated by spaces, where \(n\) is the number of elements in the array, \(k\) is the number of closest elements to find, and \(x\) is the target integer.
The second line contains \(n\) space-separated integers in ascending order representing the array \(arr\).
It is guaranteed that \(n \ge k\).
outputFormat
Output \(k\) integers in ascending order separated by a single space. These integers are the \(k\) closest elements to \(x\) in the array.
## sample5 4 3
1 2 3 4 5
1 2 3 4