My solution, O(logn + k), is easy to understand
\nclass Solution {\n    public List<Integer> findClosestElements(int[] arr, int k, int x) {\n        \n        //find the cloest element, r is the index of the 
```\nclass Solution {\n    public List<Integer> findClosestElements(int[] arr, int k, int x) {\n        \n        //find the cloest element, r is the index of the cloest element\n        int l = 0, r = arr.length-1;\n        while(l < r) {\n            int mid = (l + r)/2;\n            if(arr[mid] == x) {r = mid; break;}\n            else if(arr[mid] > x) r = mid-1;\n            else l = mid+1;\n        }\n        \n        //ensure the range\n        l = r;\n        r++;\n        while(k>0) {\n            if( r >= arr.length || (l >= 0 && x-arr[l] <= arr[r] - x) ) {\n                l--;\n            } else {\n                r++;\n            }\n            \n            k--;\n        }\n        \n        List<Integer> list =new ArrayList<>();\n        for(int i = l+1; i < r; i++) {\n            list.add(arr[i]);\n        }\n        return list;\n    }\n}\n```
[]