Minimum changes required to make all element in an array equal | C ++ program to find minimum changes required to make all elements of the array equal ; Function to count of minimum changes required to make all elements equal ; Store the count of each element as key value pair in unordered map ; Find the count of maximum occurring element ; Return count of all element minus count of maximum occurring element ; Driver code ; Function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minChanges ( int arr [ ] , int n ) { unordered_map < int , int > umap ; for ( int i = 0 ; i < n ; i ++ ) { umap [ arr [ i ] ] ++ ; } int maxFreq = 0 ; for ( auto p : umap ) { maxFreq = max ( maxFreq , p . second ) ; } return n - maxFreq ; } int main ( ) { int arr [ ] = { 2 , 3 , 3 , 4 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; cout << minChanges ( arr , n ) << ' ' ; return 0 ; }