Python solution using queue. Simplest solution imo
IntuitionStart with the input array. The problem asks us to remove elements from this input array until we have k elements left. The elements to be removed are
# Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
Start with the input array. The problem asks us to remove elements from this input array until we have k elements left. The elements to be removed are decided based on their distance from a given value x.

Since the array is sorted, the values to be removed in each iteration of removal can only be from either end of the array. This seems like a good use case of a queue data structure.

# Approach
<!-- Describe your approach to solving the problem. -->
1. Turn the input array into a queue.
2. Until this queue is not of length k, continue popping from either the left or the right of the queue.
3. Pop from the left if the value at the front of the queue is farther in distance than the value at the end of the queue. Pop from the right if the front value is as close or closer to x.

# Complexity
- Time complexity:
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
O(n)

- Space complexity:
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
O(n)

# Code
```python3 []
class Solution:
    def findClosestElements(self, arr: List[int], k: int, x: int) -> List[int]:
        q = collections.deque(arr)

        while len(q) > k:
            if abs(x - q[-1]) < abs(x - q[0]):
                q.popleft()
            else:
                q.pop()

        return list(q)
```
['Python3']