 # HIGHFREQ - Editorial

Setter: Abhinav Gupta
Tester: Jeevan Jyot Singh, Nishank Suresh
Editorialist: Yash Kulkarni

1443

Hash Maps

# PROBLEM:

Chef has an array A of length N.

Let F(A) denote the maximum frequency of any element present in the array.

For example:

• If A = [1, 2, 3, 2, 2, 1], then F(A) = 3 since element 2 has the highest frequency = 3.
• If A = [1, 2, 3, 3, 2, 1], then F(A) = 2 since highest frequency of any element is 2.

Chef can perform the following operation at most once:

• Choose any subsequence S of the array such that every element of S is the same, say x. Then, choose an integer y and replace every element in this subsequence with y.

For example, let A = [1, 2, 2, 1, 2, 2]. A few examples of the operation that Chef can perform are:

• [1, \textcolor{red}{2, 2}, 1, 2, 2] \to [1, \textcolor{blue}{5, 5}, 1, 2, 2]
• [1, \textcolor{red}{2}, 2, 1, \textcolor{red}{2, 2}] \to [1, \textcolor{blue}{19}, 2, 1, \textcolor{blue}{19, 19}]
• [\textcolor{red}{1}, 2, 2, 1, 2, 2] \to [\textcolor{blue}{2}, 2, 2, 1, 2, 2]

Determine the minimum possible value of F(A) Chef can get by performing the given operation at most once.

# EXPLANATION:

Let P be the element with the maximum frequency in A (having frequency freq_P) and Q be the element with the second maximum frequency in A (having frequency freq_Q) . It is possible that Q does not exist. Let us consider these 2 cases:

• Q exists - Initially F(A) = freq_P. We should choose x = P because we need to minimize the final value of F(A) as in the question and it is always better to have y \neq Q. We should only replace a maximum of \lfloor\frac{freq_P}{2} \rfloor occurrences of P to y because if we replace more, then y becomes the new element with the maximum frequency. Also, replacing less than \lfloor\frac{freq_P}{2} \rfloor occurrences of P to y is not optimal because frequency of P can be further reduced by replacing more occurrences of P and hence F(A) can be reduced further. Hence, it is optimal to replace \lfloor\frac{freq_P}{2} \rfloor occurrences of P to some y \neq Q. Now, freq_P - \lfloor\frac{freq_P}{2} \rfloor is the final frequency of P and freq_Q is the final frequency of Q. The maximum among these two i.e. Max( freq_P - \lfloor\frac{freq_P}{2} \rfloor, freq_Q) has to be the answer because frequency of y = \lfloor\frac{freq_P}{2} \rfloor \leq freq_P - \lfloor\frac{freq_P}{2} \rfloor = frequency of P.
• Q does not exist - Everything will remain same like the above point just freq_Q is 0. So, the answer is freq_P - \lfloor\frac{freq_P}{2} \rfloor.

This can be implemented very easily using a hash map, which stores the number of occurrences of all elements in A.

# TIME COMPLEXITY:

O(N) for each test case.

# SOLUTION:

Setter's solution
#include <bits/stdc++.h>
#define ll long long int
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define vl vector <ll>
#define all(c) (c).begin(),(c).end()
using namespace std;
ll power(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll modInverse(ll a){return power(a,mod-2);}
const int N=500023;
bool vis[N];
long long readInt(long long l,long long r,char endd){
long long x=0;
int cnt=0;
int fi=-1;
bool is_neg=false;
while(true){
char g=getchar();
if(g=='-'){
assert(fi==-1);
is_neg=true;
continue;
}
if('0'<=g && g<='9'){
x*=10;
x+=g-'0';
if(cnt==0){
fi=g-'0';
}
cnt++;
assert(fi!=0 || cnt==1);
assert(fi!=0 || is_neg==false);

assert(!(cnt>19 || ( cnt==19 && fi>1) ));
} else if(g==endd){
if(is_neg){
x= -x;
}

if(!(l <= x && x <= r))
{
cerr << l << ' ' << r << ' ' << x << '\n';
assert(1 == 0);
}

return x;
} else {
assert(false);
}
}
}
string ret="";
int cnt=0;
while(true){
char g=getchar();
assert(g!=-1);
if(g==endd){
break;
}
cnt++;
ret+=g;
}
assert(l<=cnt && cnt<=r);
return ret;
}
long long readIntSp(long long l,long long r){
}
long long readIntLn(long long l,long long r){
}
}
}
int sumN=0;
void solve()
{
sumN+=n;
assert(sumN<=200000);
int A[n+1]={0};
int freq[n+1]={0};
for(int i=1;i<=n;i++)
{
if(i!=n)
else
freq[A[i]]++;
}
vector <int> v;
for(int i=1;i<=n;i++)
v.pb(freq[i]);
sort(all(v));
reverse(all(v));
int x=v;
int y=v;
cout<<max((x+1)/2,y)<<'\n';
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL),cout.tie(NULL);
while(T--)
solve();
assert(getchar()==-1);
cerr << "Time : " << 1000 * ((double)clock()) / (double)CLOCKS_PER_SEC << "ms\n";
}

Editorialist's Solution
using namespace std;

int main() {
int T;
cin >> T;
while(T--){
int n;
cin >> n;
unordered_map<int,int>ff;
for(int i=0;i<n;i++){
int t;
cin >> t;
ff[t]++;
}
vector<int>y;
for(auto i:ff)y.push_back(i.second);
sort(y.begin(),y.end());
int m=y.size();
int a=y[m-1];
int b=0;
if(m>1)b=y[m-2];
cout << max(a/2+(int)(a%2!=0),b) << endl;
}
return 0;
}


2 Likes

I could have got this right if only I wrote max((x+1)/2,y) import math
t = int(input())
while t:
n = int(input())
arr = list(map(int, input().split()))
dict = {}
for i in arr:
if dict.get(i) == None:
dict[i] = 1
else:
dict[i]+=1
maxi = 0
sec_maxi = 0
for i in dict:
if dict[i]>=maxi:
sec_maxi = maxi
maxi = dict[i]
print(max(sec_maxi,math.ceil(maxi/2)))
t-=1


Can anyone tell what is wrong with my logic ? This code is failing three test cases.

Can anyone find a testcase for which my code fails? Sorry if it is something obvious

Solution: 70076260 | CodeChef (not the same implementation as editorialist)

Input:

1
6
2 2 2 3 3 3


Output:

2


Expected Output:

3

1 Like

“Choose any subsequence S of the array such that every element of S is the same, say x”

Thanks though

1 Like

@prakhr98 you need to add one more line for the case if the frequency is less than max freq but greater than second highest freq

if dict[i]>sec_maxi:
sec_maxi = dict[i]

setter code is not working on this testcase
1
9
1 2 2 3 2 2 4 2 2
and in question there’s nothing mentioned that I didn’t take this testcase.

@codeforces2103 - Try the following - with an empty line below the list of numbers

1
9
1 2 2 3 2 2 4 2 2

This should run