<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>& nums, int k, int x) {
        vector<int> ans;
        int n= nums.size();
        if(x<nums[0]){
            int i=0;
            while(i<k && i<n){
                ans.push_back(nums[i]);
                i++;
            }
            return ans;
        }
        if(x>nums[n-1]){
            int i=n-1;
            int count =0;
            while(i>0 && count<k){
                ans.push_back(nums[i]);
                i--;
                count++;
            }
            sort(ans.begin(), ans.end());
            return ans;
        }
        if(presentInArr(nums, x)){
            int i=0;
            while(i<n && x>nums[i]){
                i++;
            }
            int pointer1=i;
            int pointer2=i+1;
            // while(pointer2<n-1 && nums[pointer2]== nums[pointer2+1]){
            //     pointer2++;
            // }
            int count=0;
            while(pointer1>=0 && pointer2<n && count<k){
                while(pointer1>=0 && pointer2<n && count<k && x-nums[pointer1]<= nums[pointer2]-x){
                    ans.push_back(nums[pointer1]);
                    pointer1--;
                    count++;
                }
                // cout<<nums[pointer1]<<" "<<nums[pointer2]<<" "<<count<<" 44\n";
                while(pointer2<n && pointer1>=0 && count<k && x-nums[pointer1] > nums[pointer2]-x){
                    ans.push_back(nums[pointer2]);
                    pointer2++;
                    count++;
                }
            }
            while(count<k){
                while(pointer1>=0 && count<k){
                    ans.push_back(nums[pointer1]);
                    pointer1--;
                    count++;
                }
                while(pointer2<n && count<k){
                    ans.push_back(nums[pointer2]);
                    pointer2++;
                    count++;
                }
            }
            sort(ans.begin(), ans.end());
            return ans;
        } else {
            int i=0;
            while(i<n && x>nums[i]){
                i++;
            }
            int pointer1=i-1;
            int pointer2=i;
            // while(pointer2<n-1 && nums[pointer2]== nums[pointer2+1]){
            //     pointer2++;
            // }
            // while(pointer1>0 && nums[pointer1]== nums[pointer1-1]){
            //     pointer1--;
            // }
            int count=0;
            while(pointer1>=0 && pointer2<n && count<k){
                // cout<<nums[pointer1]<<" "<<nums[pointer2]<<" "<<count<<"\n";
                while(pointer1>=0 && pointer2<n && count<k && x-nums[pointer1]<= nums[pointer2]-x){
                    ans.push_back(nums[pointer1]);
                    pointer1--;
                    count++;
                }
                // cout<<nums[pointer1]<<" "<<nums[pointer2]<<" "<<count<<" 44\n";
                while(pointer2<n && pointer1>=0 && count<k && x-nums[pointer1] > nums[pointer2]-x){
                    ans.push_back(nums[pointer2]);
                    pointer2++;
                    count++;
                }
            }
            while(count<k){
                while(pointer1>=0 && count<k){
                    ans.push_back(nums[pointer1]);
                    pointer1--;
                    count++;
                }
                while(pointer2<n && count<k){
                    ans.push_back(nums[pointer2]);
                    pointer2++;
                    count++;
                }
            }
            sort(ans.begin(), ans.end());
            return ans;
        }
    }
    bool presentInArr(vector<int> nums, int x){
        for(int i=0; i<nums.size(); i++){
            if(nums[i]==x) return true;
        }
        return false;
    }
};
36500