BIANRY SEARCH with abs(............)
Approach\n\n\nGiven An integer a is closer to x than an integer b if:\n|a - x| <  |b - x|, or\n|a - x| == |b - x| and a < b\n \nExample 1:\n\nInput: nums = [1,2
## Approach\n\n```\nGiven An integer a is closer to x than an integer b if:\n|a - x| <  |b - x|, or\n|a - x| == |b - x| and a < b\n \nExample 1:\n\nInput: nums = [1,2,3,4,5], k = 4, x = 3\nOutput: [1,2,3,4]\n```\n```\nif we can find our answer\'s leftmost value which is here 1, \nthen we can easily send the rest k-1 values with 1 as a vector easily!\nThis is what we will be doing here, FINDING THE  LEFTMOST VALUE  \n```\n```\n    ___________\n   |           |\n   1   2   3   4   5     k = 4, x = 3\n       |           |\n        -----------\nWe know the answer is either 1,2,3,4 or 2,3,4,5. Let\'s see differences\n\n   1   2   3   4   5  -- values i.e. nums[i]\n   2   1   0   1   2  -- abs(value-x) i.e. differences from x\n       |_______|\n            |---> We will pick these 3 values obviously.\n                  The Q is, now 1 more value to pick, 1 or 5?\nabs(1-3) = 2 and abs(5-3) = also 2. What was our given condition?\n" if |a - x| == |b - x| and a < b "\nHere abs(1-3) = abs(5-3) = 2 BUT as 1<5, so we will pick the value 1.\n```\n```\nExample 2:\n\nInput: nums = [1,2,3,4,6], k = 4, x = 3\nOutput: [1,2,3,4]\n    ___________\n   |           |\n   1   2   3   4   6     k = 4, x = 3\n       |           |\n        -----------\nWe know the answer is either 1,2,3,4 or 2,3,4,6. Let\'s see differences\n\n   1   2   3   4   6  -- values i.e. nums[i]\n   2   1   0   1   3  -- abs(value-x) i.e. differences from x\n       |_______|\n            |---> We will pick these 3 values obviously.\n                  The Q is, now 1 more value to pick, 1 or 6?\nObviously 1 cause abs(1-3) = 2 < abs(6-3) = 3 means 1 is more closer\nto x(3) than 6. This is exactly what our first given condition is\n" if |a - x| <  |b - x| "\n\n                    nums = [1,2,3,4,6], k = 4, x = 3\nLet\'s say left = 0,right = 1,mid = 0 (indexes), means nums[mid] = 1.\nNow standing on the value 1 we\'ve to check if 6 is more closer to x than \n1 to x, right? \nif abs(6-3) > abs(1-3) means if 6 is less closer to 3 than 1 to 3\nthe answer will start from 1 (1,2,3,4), so write right = mid = index 0\nand loop is stopped!\n\nFor the first example : abs(5-3) == abs(1-3) means  5 and 1 both \ncloser with same difference 2. But as 1<5\nthe answer will start from 1 (1,2,3,4), so write right = mid = index 0\nand loop is stopped!\n\nSo we can clearly say \n     if (abs(nums[mid+k]-x) >= abs(nums[mid]-x))\n            right = mid\n     else\n            left = mid+1\n         \n```  \n```\nExample 3 :\nFor [1,1,2,2,2,2,3,3], k=3, x=3 those condition fails.\n     0 1 2 3 4 5 6 7 -- (indexes) \n\nIf mid = 2, nums[mid] = 2, nums[mid+k] = 2\nif (abs(nums[mid+k]-x) >= abs(nums[mid]-x))\nif (abs(2-2) >= abs(2-2)) right = mid = 2 but our answer is 2,3,3 : \nindex starts from 5 but here right became 2. But we need to go \nright by left = mid+1 = 3 to reach our correct answer? HOW?\n```\n```\nExample 4 :\n            1 2 3 4 5 6 7 8 9  k = 3, x = 7\n            0 1 2 3 4 5 6 7 8  -- (indexes) \n\nLet\'s say mid = 1, so nums[mid] = 2, nums[mid+k] = 5\nHere abs(5-7) = 2 and abs(2-7) = 5, as you can see for left values of 7\n        \'1 2 3 4 5 6\'     7      8 9  k = 3, x = 7\n         0 1 2 3 4 5      6      7 8  -- (indexes) \n\n         1 2 3 4 5 6\nif mid = 1 and mid+k = 4, so nums[mid] = 2 and nums[mid+k] = 5 \ni.e. if nums[mid+k] in the ranges of the values less than x \nthen,\n     abs(nums[mid]-x) >= abs(nums[mid+k]-x) \n     abs(2-7) = 5 >= abs(5-7) = 2\nwhich is the opposite of our if condition.\n\nif mid = 5 and mid+k = 8, so nums[mid] = 6 and nums[mid+k] = 9 \ni.e. if nums[mid+k] in the ranges of the values greater than or \nequals to x, then,\n     abs(nums[mid+k]-x) >= abs(nums[mid]-x)\n     abs(9-7 = 2) >= abs(6-7) = 1\n\nSO WHEN IF (abs(nums[mid+k]-x) >= abs(nums[mid]-x)) CONDITION WILL BE \nMATCHED? WHEN NUMS[MID+K]>=X ? EXACTLY! \n\n```\n```\nSo we can clearly say to avoid error like happened in example 3 \n     \' if (abs(nums[mid+k]-x) >= abs(nums[mid]-x) && nums[mid+k]>=x)\n            right = mid\n       else\n            left = mid+1 \n```\n\n### If my solution helped you, an upvote will really make me happy. Have  a good day \uD83D\uDE0A\n### Feel free to ask any Q.\n\n### Python\n```\nclass Solution:\n    def findClosestElements(self, nums: List[int], k: int, x: int) -> List[int]:\n        left, right, mid = 0, len(nums)-k, 0\n        while left<right :\n            mid = left + ((right-left)>>1)\n            if abs(nums[mid+k]-x) >= abs(nums[mid]-x) and nums[mid+k]>=x :\n                right = mid\n            else :\n                left = mid+1\n\n        return nums[left:left+k]\n```\n###  C++\n```\nclass Solution \n{ \npublic:\n    vector<int> findClosestElements(vector<int>& nums, int k, int x) \n    {\n        int left=0,right=nums.size()-k,mid;\n        while(left<right)\n        {   \n            mid = left + (right-left)/2;\n            abs(nums[mid+k]-x)>=abs(nums[mid]-x) && nums[mid+k]>=x? right=mid : left=mid+1;\n        }\n        return vector<int>(nums.begin()+left,nums.begin()+left+k);   \n    }\n};\n```
['C++', 'Python3']