Given a sorted integer array of unique elements, find the k-closest elements to a given target in the array where the values k and target are given as positive integers.
Note that the target may or may not be present in the input array. If the target is less than or equal to the first element in the input array, return the first k elements. Similarly, if the target is more than or equal to the last element in the input array, return the last k elements. The returned elements should be in the same order as present in the input array, and if there are two elements that are both k away from the target, then select the lower of the two for the output.
Sample Executions:
Input: [10, 12, 15, 17, 18, 20, 25], k = 4, target = 16
Output: 12, 15, 17, 18
Input: [110, 12, 14, 17, 20, 251], k = 3, target = 16
Output: 12, 14, 17
Input: [2, 3, 4, 5, 6, 7], k = 3, target = 1
Output: 2, 3, 4
Input: [2, 3, 4, 5, 6, 7], k = 4, target = 8
Output: 4, 5, 6, 7
Clearly and completely describe your algorithm and analyze its complexity. Obviously, the more efficient the algorithm, the higher your score.