Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- #define ff first
- #define ss second
- #define ll long long
- #define pb push_back
- #define mp make_pair
- #define pii pair<int,int>
- #define vi vector<int>
- #define mii map<int,int>
- #define pqb priority_queue<int>
- #define pqs priority_queue<int,vi,greater<int> >
- #define setbits(x) __builtin_popcountll(x)
- #define zrobits(x) __builtin_ctzll(x)
- #define mod 1000000007
- #define inf 1e18
- #define ps(x, y) fixed<<setprecision(y)<<x
- #define mk(arr, n, type) type *arr=new type[n];
- #define w(x) int x; cin>>x; while(x--)
- #define pw(b, p) pow(b,p) + 0.1
- void fastIO() {
- ios_base::sync_with_stdio(false);
- cin.tie(NULL);
- }
- int main() {
- fastIO();
- w(t){
- int n;cin>>n;
- vi arr(n);
- for(int &i:arr) cin>>i;
- //find (num,freq) pair
- mii myMap;
- for(int i:arr)
- myMap[i]++;
- //find max freq
- int maxFreq =0;
- for(auto i:myMap){
- if(i.ss>maxFreq){
- maxFreq=i.ss;
- }
- }
- vi res;
- //find how many numbers have the same maxFreq
- for(auto i:myMap){
- if(i.ss==maxFreq)
- res.pb(i.ff);
- }
- ll totalDiff=INT_MAX,tempDiff=0;
- //for every such x , find diff and comapre
- for(int x:res){
- tempDiff=0;
- for(auto i:myMap)
- tempDiff+=abs(i.ff - x)*i.ss;
- totalDiff=min(totalDiff,tempDiff);
- }
- cout<<totalDiff<<"\n";
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement