<p>Given a <strong>sorted</strong> integer array <code>arr</code>, two integers <code>k</code> and <code>x</code>, return the <code>k</code> closest integers to <code>x</code> in the array. The result should also be sorted in ascending order.</p>

<p>An integer <code>a</code> is closer to <code>x</code> than an integer <code>b</code> if:</p>

<ul>
	<li><code>|a - x| &lt; |b - x|</code>, or</li>
	<li><code>|a - x| == |b - x|</code> and <code>a &lt; b</code></li>
</ul>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> arr = [1,2,3,4,5], k = 4, x = 3
<strong>Output:</strong> [1,2,3,4]
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> arr = [1,2,3,4,5], k = 4, x = -1
<strong>Output:</strong> [1,2,3,4]
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= k &lt;= arr.length</code></li>
	<li><code>1 &lt;= arr.length &lt;= 10<sup>4</sup></code></li>
	<li><code>arr</code> is sorted in <strong>ascending</strong> order.</li>
	<li><code>-10<sup>4</sup> &lt;= arr[i], x &lt;= 10<sup>4</sup></code></li>
</ul>

class Solution {
public:
    vector<int> brute(vector<int> arr, int k, int x){
        vector<int> sol;
        sort(arr.begin(), arr.end(), [x](int &a, int &b){
            return abs(x - a) < abs(x - b) ;
        });
        for(int i = 0; i<k; i++){
            sol.push_back(arr[i]);
        }
        sort(sol.begin(), sol.end());
        return sol;
    }

    vector<int> twopointermethod(vector<int> arr, int k, int x){
        int low = 0, high = arr.size()-1;
        while(high-low >=k){
            if(x - arr[low] > arr[high] -x){
                low++;
            }
            else{
                high--;
            }
        }

        // vector<int> ans;
        // while(low<=high){
        //     ans.push_back(arr[low]);
        //     low++;
        // }
        return vector<int>(arr.begin() + low, arr.begin() + high+1);
    }

    int lowerbound(vector<int> arr, int x){
        int s= 0, e = arr.size()-1, mid = (e+s)>>1;
        int ans = e;
        while(s<=e){
            if(arr[mid] >= x){
                ans = mid;
                e = mid-1;
            }
            else if(x>arr[mid]){
                s = mid+1;
            }
            else{
                e = mid-1;
            }
            mid = (s+e) >>1;
        }
        return ans;
    }

    vector<int> bsplustwoptr(vector<int> arr, int k , int x){
        //Lower bound
        int high = lowerbound(arr, x);
        int low = high -1;

        while(k--){
            if(low < 0){
                high++;
            }
            else if(high >= arr.size()){
                low--;
            }
            else if(x - arr[low] > arr[high] - x){
                high++;
            }
            else{
                low--;
            }
        }
        return vector<int>(arr.begin() + low+1, arr.begin() + high);
    }

    vector<int> findClosestElements(vector<int>& arr, int k, int x) {
        // return brute(arr, k, x);
        // return twopointermethod(arr, k, x);
        return bsplustwoptr(arr, k, x);
    }
};
36000