C++ | Sorting with lambda function | Short and Simple
Please upvote if you like my solution\nAnd feel free to share your opinion.\n\n\nclass Solution {\npublic:\n    vector<int> findClosestElements(vector<int>& arr
*Please upvote if you like my solution\nAnd feel free to share your opinion.*\n\n```\nclass Solution {\npublic:\n    vector<int> findClosestElements(vector<int>& arr, int k, int x) {\n        vector<int>ans;            //vector to store result\n\t\t\n\t\t//Sorting using lambda function\n        sort(arr.begin(),arr.end(),[x](int a,int b){\n            if(abs(a-x)==abs(b-x))               //if difference is equal choose the smaller value\n                return a<b;\n            return abs(a-x)<abs(b-x);          //else choose the value which has smaller difference\n        });\n\t\t\n        ans=vector<int>(arr.begin(),arr.begin()+k); //slice the array to size k\n        sort(ans.begin(),ans.end()); //arranging values in ascending order\n        return ans;   \n    }\n};\n```
['C', 'Sorting']