Problem:-
Problem Paraphrase:-
Given an array A, select x such that x is having higher frequency when 2 conditions are applied.
1 → No adjacent values should be selected/counted once, like \{3, 3, 3\} here count of 3's is 2.
2- > When there exists 2 equal counts, then select the count formed with smaller number.
Approach:-
While reading values, count frequencies. And while iterating through give array, when a_i = a_{i+1}, then decrease the count in the frequencies table and move to the 2^{nd} element from i, that is.
a_i = a_{i+1} \large\Rightarrow \normalsize freq[a_i]-=1 \large\Rightarrow \normalsize i +=2
Submission:-
Giving me WA for the below code
https://www.codechef.com/viewsolution/32374540
My Code:-
#include <bits/stdc++.h>
using namespace std;
int main(void)
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
unordered_map<int, int> m;
int t, n, i, ans, freq;
cin >> t;
while( t-- )
{
cin >> n;
int a[n+1];
for(i=0; i<n; i++)
{
cin >> a[i];
m[a[i]]++;
}
for(i=a[n]=0; i<n; i++)
{
if( a[i] == a[i+1] )
{
m[a[i]]--;
i++;
}
}
ans = freq = 0;
for(auto x: m)
{
if( (x.first < ans and x.second >= freq) or ans == 0 )
{
ans = x.first;
freq = x.second;
}
}
cout << ans ;
if( t )
{
cout << '\n' ;
m.clear();
}
}
return 0;
}
Thanks for your time, you helped me grow .