We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
intmain(){intt;cin>>t;while(t--){intn;cin>>n;vector<int>a(n);for(inti=0;i<n;i++)cin>>a[i];// giving x chocs to every colleague other than chosen one // is the same as taking away x chocs from the chosen oneintmin=*min_element(a.begin(),a.end());intnumops=0;// consider taking away 5 chocs first (nChocs)for(inti=0;i<n;i++){intnChocs=floor((a[i]-min)/5.0);a[i]-=(5*floor((a[i]-min)/5.0));numops+=nChocs;}min=*min_element(a.begin(),a.end());// min of leftovers in a[]// fine tuning for last calc, get freq of diff firstvector<int>freq(5);// stores diff of 0 to 4for(inti=0;i<n;i++)freq[a[i]-min]++;// case of min num of chocs is minintextra_numops=1*(freq[1]+freq[2])+2*(freq[3]+freq[4]);// case of min num of chocs is min-1intextra_numops1=1*(freq[0]+freq[1]+freq[4])+2*(freq[2]+freq[3]);if(extra_numops1<extra_numops)extra_numops=extra_numops1;// case of min num of chocs is min-2intextra_numops2=1*(freq[0]+freq[3])+2*(freq[1]+freq[2]+freq[4]);if(extra_numops2<extra_numops)extra_numops=extra_numops2;cout<<numops+extra_numops<<endl;}return0;}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Equal
You are viewing a single comment's thread. Return to all comments →
Passed all tests: