Minimize swaps required to maximize the count of elements replacing a greater element in an Array | C ++ Program to implement the above approach ; Function to find the minimum number of swaps required ; Stores the frequency of the array elements ; Stores maximum frequency ; Find the max frequency ; Update frequency ; Update maximum frequency ; Driver Code ; function call
#include <bits/stdc++.h> NEW_LINE using namespace std ; int countSwaps ( int A [ ] , int n ) { map < int , int > mp ; int max_frequency = 0 ; for ( int i = 0 ; i < n ; i ++ ) { mp [ A [ i ] ] ++ ; max_frequency = max ( max_frequency , mp [ A [ i ] ] ) ; } return n - max_frequency ; } int main ( ) { int A [ ] = { 6 , 5 , 4 , 3 , 2 , 1 } ; cout << countSwaps ( A , 6 ) ; return 0 ; }