O(n) time | two pointers | C++
Here we are reducing the limits until we get our k elements.\nWhen you find abs(arr[right] - x) is >= abs(arr[left] - x) \nthen you should decrement right and p
Here we are reducing the limits until we get our k elements.\nWhen you find abs(arr[right] - x) is >= abs(arr[left] - x) \nthen you should decrement right and prefer left \nelse decrement left and prefer right \nas array is already sorted.\n\n```\nclass Solution {\npublic:\n    vector<int> findClosestElements(vector<int>& arr, int k, int x) {\n        int lf = 0,rgt = arr.size()-1;\n        while((rgt-lf)>=k){\n            if(abs(arr[rgt]-x) >= abs(arr[lf]-x))\n                rgt--;\n            else\n                lf++;\n        }\n        vector<int>ans(k);\n        int cur = 0;\n        for(int i=lf;i<=rgt;i++)\n            ans[cur++] = arr[i];\n        return ans;\n    }\n};\n```
['Two Pointers', 'C']