[C++/Java/Python] Binary Search - With Diagram explain - Clean & Concise
Idea\n- We need to find k closest elements to x in the sorted array.\n- If ith element is one of the elements closest to x, then i+kth element should not be in 
**Idea**\n- We need to find `k` closest elements to `x` in the sorted array.\n- If `ith` element is one of the elements closest to x, then `i+k`th element should not be in the answer.\n- Let `f[i] = abs(x - arr[i])`, where `0 <= i < n`, we have chart like the following picture.\n- We need to choose the staring index `startIdx` so that `f[startIdx...startIdx+k-1]` is the k smallest values.\n- From the chart, we can see that:\n   - `i` must be on the left side of the chart\n   - and `i+k` must be on the right side of the chart.\n- Then the corresponding absolute values are:\n   - `x - arr[i]` for `ith`\n   - and `arr[i+k] - x` for `i+k`th\n- Since `i` on the left side and `i+k` on the right side of the chart, we can see that:\n   - If `i` increase then `i+k` element will be far compare to `x`\n   - If `i` decrease then `i+k` element will be close compare to `x`\n=> We can binary search to find the left most `startIdx` where `f[startIndx] <= f[startIdx+k]`.\n\n![image](https://assets.leetcode.com/users/images/2d9e0935-0754-4e3f-a64b-fb45c8b4e993_1625228819.0449612.png)\n\n**Implementation**\n<iframe src="https://leetcode.com/playground/Gv5bKZCX/shared" frameBorder="0" width="100%" height="380"></iframe>\n\n**Complexity:**\n- Time: O`(log(N-K) + K)`\n- Space: `O(K)`\n\n**Simillar problems**\n* [33. Search in Rotated Sorted Array](https://leetcode.com/problems/search-in-rotated-sorted-array/)\n\n\nIf you think this post is useful, please help to **vote** so this post can reach more audience. Any questions or discussions in this post are welcome! Thanks.
['Binary Search']