class Solution {

public:

int minMoves2(vector<int>& nums) {

int mid = nums.size()/2;

nth_element(nums.begin(), nums.begin()+mid, nums.end());

```
int x= nums[mid], res=0;
for (auto n:nums)
res += abs(n-x);
return res;
}
```

};

//use quick select by nth_element().