ENCJMAY2-Editorial

PROBLEM LINK:

Practice
Contest Link

Author:Abhijeet Trigunait
Tester:Sandeep Singh,Arnab Chanda

DIFFICULTY:

CAKEWALK

PREREQUISITES:

Knowledge of Array.

PROBLEM:

You are given array of integers.You have to reduce the array until all remaining elements are equal. Determine the minimum number of operation to make the array elements equal.

EXPLANATION:

In order to solve the problem in minimum operation we can just let the element in array which has maximum count because that would cause less removal operation in comparision to other elements of array.So find the element with maximum occurrences and subtract that number of occurrences from the initial array size to determine the minimum number of elements to be removed.

SOLUTION:

Setter's Solution
#include <bits/stdc++.h> 
using namespace std; 

int maxCount(int arr[], int n) 
 { 

    unordered_map<int, int> counter; 
    for (int i = 0; i < n; i++) 
	    counter[arr[i]]++; 
    int max_count = 0, max_element = -1; 
    for (auto i : counter) { 
        	if (max_count < i.second) { 
		    max_element = i.first; 
		    max_count = i.second; 
	    } 
    } 

    return max_count; 
} 


int main() 
{ 
    int testcase;
    cin>>testcase;
    while(testcase--){
    int n,ans;
    cin>>n;
    int arr[n];
    for(int i=0;i<n;++i)
    cin>>arr[i];
    ans=maxCount(arr,n);
    cout<<n-ans<<endl;
    }
} 
Tester's Solution
#include <bits/stdc++.h>
#define ll long long int
#define max(a,b) (a>b?a:b)
#define min(a,b) (a<b?a:b)
#define scanarr(a,b,c) for( i=b;i<c;i++)cin>>a[i]
#define showarr(a,b,c) for( i=b;i<c;i++)cout<<a[i]<<' '
#define ln cout<<'\n'
#define FAST ios_base::sync_with_stdio(false);cin.tie(NULL);
#define mod 1000000007
#define MAX 100005
using namespace std;
////////////////////////////////////////////////////////////////CODE STARTS 
HERE////////////////////////////////////////////////////////////////

void solve(){
    int n,i,j;
    cin>>n;
    int tmp;
    unordered_map <int, int> mp;
    int mx =0;

    for(i = 0; i < n; ++i){
        cin >> tmp;
        mp[tmp]++;
        mx = max(mx, mp[tmp]);
    }

    cout << n - mx<<endl;;



 }
int main(){
    #ifndef ONLINE_JUDGE
    freopen("input.txt","r",stdin);
    #endif
    int t;
    cin>>t;
    while(t--)
     solve();
} 
2 Likes