Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- vector<int> findClosestElements(vector<int>& arr, int k, int x) {
- vector<pair<int, int>> ans;
- for(auto e : arr)
- {
- ans.push_back({e, abs(e - x)});
- }
- stable_sort(begin(ans), end(ans), [](auto& left, auto& right)
- {
- return left.second < right.second;
- });
- vector<int> res;
- for(int i = 0; i < k; i++)
- {
- res.push_back(ans[i].first);
- }
- sort(begin(res), end(res));
- return res;
- }
- };
Add Comment
Please, Sign In to add comment