Find K Closest Elements LeetCode solution in Java
1class Solution {2public List<Integer> findClosestElements(int[] arr, int k, int x) {3List<Integer> result = new ArrayList<>();45int low = 0, high = arr.length - 1;67while (high - low >= k) {8if (Math.abs(arr[low] - x) > Math.abs(arr[high] - x)) low++;9else high--;10}1112for (int i = low; i <= high; i++) result.add(arr[i]);1314return result;15}16}