<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> findClosestElements(vector<int>& arr, int k, int x) {
        // just subtract x from each element O(n), increase and increase, sort this using merge sort - O(n), get final indices
        // pick k O(n), find the transition point, but maintain the indices instead of the actual numbers, that should be easy. But wait, can we do it in O(k)?
        // wait wait, we have to return in ascending, yeah, it will be in ascending right? no, it will not be
        // how do we ensure it is ascending, sort later klogk, instead, can we somehow ensure while arranging the elements that it is O(k), 
        // std::cout << "what" << std::endl;
        vector<int> arr2;
        
        std::for_each(arr.begin(),arr.end(),[&arr2,&x](const int& e){arr2.emplace_back(e-x);});
        
        int idx = findTransitionPoint(arr2);
        
        std::for_each(arr2.begin(),arr2.end(),[](int& e){e = std::abs(e);});
        
        auto indices = getTopKIndices(idx, arr2, k);
        
        std::sort(indices.begin(),indices.end());
        vector<int> ans;
        std::for_each(indices.begin(),indices.end(),[&ans,&arr](const int& idx){ans.emplace_back(arr[idx]);});
        return ans;
    }



    vector<int> getTopKIndices(const int& idx, const vector<int>& arr2, const int& k)
    {
        vector<int> indices;
        //0 to idx, idx+1 to arr2.size()-1
        int firstPtr=idx;
        int secondPtr=idx+1;
        while(indices.size()<k && firstPtr>=0 && secondPtr<=arr2.size()-1)
        {
            if(arr2[firstPtr]<=arr2[secondPtr]){
                indices.emplace_back(firstPtr);
                firstPtr--;
            }
            else
            {
                indices.emplace_back(secondPtr);
                secondPtr++;
            }
        }

        while(indices.size()<k && firstPtr>=0) indices.emplace_back(firstPtr--);
        while(indices.size()<k && secondPtr<=arr2.size()-1) indices.emplace_back(secondPtr++);
        return indices;
    }

    int findTransitionPoint(vector<int>& arr)
    {
        if(arr.front()>=0) return -1;
        else if(arr.back()<=0) return arr.size()-1;
        int idx = -1;
        for(int i=0;i<arr.size()-1;++i)
        {
            if(arr[i]<=0 && arr[i+1]>=0)
            {
                idx = i;
                break;
            }
        }
        return idx;
    }
};
39700