Java || Binary Search || 3ms || beats 97% || T.C - O(logn + k) S.C - O(1)
\n    // O(logn + k) O(1)\n\tpublic List findClosestElements(int[] arr, int k, int x) {\n\n\t\tList ans = new ArrayList<>();\n\n\t\tint len = arr.length, lo = 0
\n    // O(logn + k) O(1)\n\tpublic List<Integer> findClosestElements(int[] arr, int k, int x) {\n\n\t\tList<Integer> ans = new ArrayList<>();\n\n\t\tint len = arr.length, lo = 0, hi = len - 1, left = -1, right = len;\n\n\t\twhile (lo <= hi) {\n\t\t\tint mid = (lo + hi) / 2;\n\t\t\tif (arr[mid] == x) {\n\t\t\t\tleft = mid;\n\t\t\t\tright = mid + 1;\n\t\t\t\tbreak;\n\t\t\t} else if (arr[mid] > x) {\n\t\t\t\tright = mid;\n\t\t\t\thi = mid - 1;\n\t\t\t} else {\n\t\t\t\tleft = mid;\n\t\t\t\tlo = mid + 1;\n\t\t\t}\n\t\t}\n\n        \n\t\twhile (left >= 0 && right < len && k >= 1) {\n\t\t\tif (Math.abs(arr[left] - x) <= Math.abs(arr[right] - x))\n\t\t\t\tleft--;\n\t\t\telse\n\t\t\t\tright++;\n\t\t\tk--;\n\t\t}\n\n\t\twhile (left >= 0 && k >= 1) {\n\t\t\tleft--;\n\t\t\tk--;\n\t\t}\n\n\t\twhile (right < len && k >= 1) {\n\t\t\tright++;\n\t\t\tk--;\n\t\t}\n\n\t\tfor (int i = left + 1; i < right; i++)\n\t\t\tans.add(arr[i]);\n\n\t\treturn ans;\n\t}
[]