Using Pair type of Vector || Beats 100% of users with C++ || Faster Solution || Easy to Understand
Intuition\n Describe your first thoughts on how to solve this problem. \n\nif you like the approach please upvote it\n\n\n\n\n\n\nif you like the approach pleas
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\nif you like the approach please upvote it\n\n\n\n![image.png](https://assets.leetcode.com/users/images/7495fea3-7639-48a4-8ad9-79dc9237db4e_1701793058.7524364.png)\n\n\nif you like the approach please upvote it\n\n\n\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nUsing Pair type vector\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n    vector<int> findClosestElements(vector<int>& arr, int k, int x) \n    {\n        vector<int>ans;\n        vector<pair<int,int>>v;\n        for(int i=0;i<arr.size();i++)\n            v.push_back(make_pair(abs(x-arr[i]),arr[i]));\n        sort(v.begin(),v.end());\n        for(int i=0;i<k;i++)\n        {\n            ans.push_back(v[i].second);\n        }\n        sort(ans.begin(),ans.end());\n        return ans;\n    }\n};\n```
['Array', 'Two Pointers', 'Binary Search', 'Sliding Window', 'Sorting', 'Heap (Priority Queue)', 'C++']