Equalize the Array
time complexity O(n)
Easy one different approach... Learning Map
Array . If we delete and , all of the elements in the resulting array, , will be equal. Deleting these elements is minimal because our only other options would be to delete elements to get an array of either or . Thus, we print on a new line, as that is the minimum number of deletions resulting in an array where all elements are equal.
#include<stdio.h>
#include<map>
#include <iostream>
using namespace std;
int main()
{
int n,i;
int arr[100];
scanf("%d",&n);
std::map<int,int> newmap;
std::map<int,int>::iterator it;
for(i=0;i<n;i++)
{
scanf("%d",&arr[i]);
}
for(i=0;i<n;i++)
{
if (newmap.find(arr[i]) == newmap.end())
{
newmap.insert ( std::pair<int,int>(arr[i],0));
}
else
{
newmap[arr[i]] ++;
}
}
int current_max=0;
for (it=newmap.begin(); it!=newmap.end(); ++it)
{
if( it->second>current_max)
{
current_max=it->second;
}
}
printf("%d",n-(current_max+1));
return 0;
}
No comments:
Post a Comment