the function equala has been used to check if all inputs are same , eg for cases like 1 1 1 1etc.
here i am basically checking if the case is :-1. all equal (in which case it outputs zero)
2. not 1. , here i my output will be =(n-max), where max is the frequency of the most occuring element.
Works for sample testcase.
Link for problem:- CodeChef: Practical coding for everyone
Solution:-
#include
#include <unordered_map>
using namespace std;
bool equala(int a[],int n) ;
int main(){
int testcase=0,n=0,max=0,max1=0;
int a[100]={0},b[100]={0};
cin>>testcase;
for(int x=0;x<testcase;x++)
{ int n=0,max=0;
int a[100]={0},b[100]={0};
cin>>n;
for(int y=0;y<n;y++)
{
cin>>b[y];
a[(b[y]-1)]++;
if(b[y]>=max1)
max1=b[y];
}
int p=sizeof(b)/sizeof(b[0]);
if(equala(b,p))
cout<<“0”<<endl;
else
{ for(int y=0;y<max1;y++)
{
if(a[y]>=max)
{
max=a[y];
}
}
}
cout<<n-max<<endl;
}
return 0;
}
bool equala(int a[],int n)
{
unordered_map<int,int> m;
for(int i=0;i<n;i++)
m[a[i]]++;
if(m.size()==1)
return true;
else
return false;
}