#include <cmath>
#include <algorithm>
#include <iterator>
class Solution {
public:
vector<int> findClosestElements(vector<int>& arr, int k, int x) {
multimap<int,int> v;
vector<int> ret;
for(int i=0; i< arr.size(); i++)
{
v.insert({abs(arr[i]-x), i});
}
int count = 0;
for(auto &[key,val]: v)
{
if(count >= k)
break;
ret.push_back(arr[val]);
count++;
}
std::sort(ret.begin(), ret.end());
return ret;
}
};
No comments:
Post a Comment