Simple solution | Beginner friendly | Binary Search
Intuition\nWe need to find a window of indices such that the elements within the window is the answer.\n Describe your first thoughts on how to solve this probl
# Intuition\nWe need to find a window of indices such that the elements within the window is the answer.\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n1. Search for the target(x) [Binary Search]\n2. Get the index of the target or index of the next smallest element from the target.\n3. If the target is present we know that the closest elemenet to the target is the target itself in the given array. \n    Else check for the absolute difference between adjacent elements of the target and consider the element with lesser difference.\n4. Search for k elements\n \n    Case 1: Target is in the middle of the array:\n        Move left and right from middle until the conditions are satisfied and k elements are found\n\n    Case 2: target is towards right of the array\n        Move left from middle until k elements are found.\n\n    Case 3: target is towards left of the array\n        Move right from middle until k elements are found\n\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: O(k+log(n))\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n\n    vector<int> findClosestElements(vector<int>& arr, int k, int x) {\n        if(arr.size()==1)\n            return arr;\n\n        // binary search the position of the element\n        int left = 0, right = arr.size()-1, mid;\n        while(left < right)\n        {\n            mid = left + (right - left) / 2;\n            // if x is present point to x or else find the lesser number\n            if(arr[mid] == x || (arr[mid] < x && arr[mid+1] > x))\n                break;\n            else if(arr[mid]<x)\n                left = mid + 1;\n            else\n                right = mid;\n        }\n\n        // ensure that the element arr[mid] is present in the answer vector\n        if( abs(x - arr[mid]) > abs(x - arr[mid+1]) )\n            mid+=1;\n\n        --k; // we already know that the mid element is present in the answer\n        \n        // start from the two sides of mid as mid is already included\n        left=mid-1;\n        right=mid+1;\n        \n        // find the window where the answer is until k is 0\n        while(k && left>-1 && right<arr.size())\n        {\n            if(x - arr[left] > arr[right] - x)\n                ++right;\n            else\n                --left;\n            --k;           \n        }\n        while(left>-1 && k)\n        {\n            --left;\n            --k;\n        }\n        while(right<arr.size() && k)\n        {\n            ++right;\n            --k;\n        }\n\n        return vector<int>( arr.begin()+left+1, arr.begin()+right );\n    }\n};\n```\n\n\n\nPlease upvote if you like the solution!
['Array', 'Binary Search', 'C++']