Equalize the Array
Code
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n,i,j,count=1,max=0;
cin>>n; // input size of the array
int a[n];
for(i=0;i<n;i++) // input array
{
cin>>a[i];
}
for(i=0;i<n-1;i++) // sorting the array
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
int x;
x=a[i];
a[i]=a[j];
a[j]=x;
}
}
}
for(i=0;i<n-1;i++)
{
count=1;
for(j=i+1;j<n;j++) // counting the max appearance of element in the array
{
if(a[i]==a[j])
{
count++;
}
else{break;}
}
if(max<count) // saving the max value of appearance
{
max=count;
}
}
cout<<n-max; // the maximum appeared element will stay and the remaining elements must be deleted to get the answer hence the number of element need to be delete is n-max
return 0;
}
0 Comments