fork(1) download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. int arr[] = {1 , 2, 1, 4, 3, 1};
  6. int n = 6;
  7.  
  8. unordered_map<int, int> ump; // The first int stores the value of the integer and the second int stores its frequency
  9. for (int i = 0; i < n; ++i) {
  10. ump[arr[i]]++;
  11. }
  12.  
  13. int maxfreqsofar = 0; //maxfreqsofar is the highest frequency found so far
  14. for (auto x : ump) {
  15. // If the frequency of x is greater than maxfreq then x is the current highest frequent element
  16. if (x.second > maxfreqsofar) {
  17. maxfreqsofar = x.second;
  18. }
  19. }
  20.  
  21. cout << "The minimum number of operations required to make all elements equal is: " << (n - maxfreqsofar) << endl;
  22.  
  23. }
Success #stdin #stdout 0.01s 5408KB
stdin
Standard input is empty
stdout
The minimum number of operations required to make all elements equal is: 3