Guest User

Untitled

a guest
Feb 23rd, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.62 KB | None | 0 0
  1. class Solution {
  2. public:
  3. vector<int> findClosestElements(vector<int>& arr, int k, int x) {
  4. vector<pair<int, int>> ans;
  5.  
  6. for(auto e : arr)
  7. {
  8. ans.push_back({e, abs(e - x)});
  9. }
  10.  
  11. stable_sort(begin(ans), end(ans), [](auto& left, auto& right)
  12. {
  13. return left.second < right.second;
  14. });
  15.  
  16. vector<int> res;
  17.  
  18. for(int i = 0; i < k; i++)
  19. {
  20. res.push_back(ans[i].first);
  21. }
  22.  
  23. sort(begin(res), end(res));
  24.  
  25. return res;
  26. }
  27. };
Add Comment
Please, Sign In to add comment