C++ | Two Pointer | Related Problems
Approach\n- With help of binary search, find the index j where the value is equal to greater than x. We will keep i pointer at j - 1 position. lower_bound() in 
Approach\n- With help of `binary search`, find the index `j` where the value is equal to greater than `x`. We will keep i pointer at `j - 1` position. `lower_bound()` in C++ finds that index in `log(N)` time.\n- Then we will extend our j pointer to right and `i` pointer to left as per given condition.\n- We will stop when we found `k` element. Or The difference between j and i is k.\n\n```cpp\nvector<int> findClosestElements(vector<int>& A, int k, int x) {\n        int N = A.size();\n        int j = lower_bound(begin(A), end(A), x) - begin(A);\n        int i = j - 1;\n        \n        while (j - i - 1 < k) {\n            if (i == -1 || (j < N && abs(A[j] - x) < abs(A[i] - x))) j++;\n            else i--;\n        }\n        \n        return vector<int>(begin(A) + i + 1, begin(A) + j);\n    }\n```\n\n**Two Pointer Related Problems**\n[1. Two Sum ](https://leetcode.com/problems/two-sum/)\n[2. Container With Most Water ](https://leetcode.com/problems/container-with-most-water/)\n[3. 3Sum ](https://leetcode.com/problems/3sum/)\n[4. Minimum Window Substring ](https://leetcode.com/problems/minimum-window-substring/)\n[5. Remove Duplicates from Sorted List ](https://leetcode.com/problems/remove-duplicates-from-sorted-list/)\n[6. Reorder List ](https://leetcode.com/problems/reorder-list/)\n[7. Maximum Product Subarray ](https://leetcode.com/problems/maximum-product-subarray/)\n[8. Palindrome Linked List ](https://leetcode.com/problems/palindrome-linked-list/)\n[9. Subarray Product Less Than K ](https://leetcode.com/problems/subarray-product-less-than-k/)\n[10. Backspace String Compare ](https://leetcode.com/problems/backspace-string-compare/)\n
['Two Pointers', 'C', 'Binary Tree']