HELP IN MODEFREQ

QUESTION:- CodeChef: Practical coding for everyone

MY SOLUTION :- CodeChef: Practical coding for everyone

GETTING WA??

APPROACH:- sorted the array then counted the frequency of each element, stored these frequency in an arraylist then similarly counted the frequency of each element in list and stored maximum, if there are multiple maximums stored the minimum value in res variable.

Could u explain ur approach? Btw I would like to suggest my answer, first count the frequency of all number in an array called freq[s](s<=n, s==n when all the elements are different). Since n<=10^5, just create another array freq_of_freq to find frequency of all element in the frequency array, now find the maximum in freq_of_freq array. Once u find the maximum, search the first occurence of maximum in freq and print its position

I am to getting error ;CodeChef: Practical coding for everyone
SIGABRT error
I have seen it many times but I am not getting what’s the problem in it
please help me

You gave wrong link

sorted the array then counted the frequency of each element stored these frequency in an arraylist then similarly counted the frequency of each element in list and stored maximum if there are two maximums stored the minimum on in res variable.

read this might help :CodeChef: Practical coding for everyone

there is nothing in there, please send the proper link again

https://www.codechef.com/viewsolution/37287056
this is my solution???

i got that but i cant find error in my solution

by declaring vectora(10,0); you are defining the vector of size(10) and assigning value of N
its giving a error:
Use : vectora(n);

Use map

Sorry IDK java :slight_smile:

try this
int x;
vector a(11,0);
for(i=0;i<n;i++)
{
cin>>x;
a[x]++;
}

its 11,0 because x can also be 10

i have a small assignment to finish, i’ll check ur code after 12am and tell it.

3 Likes

ok, I’ll wait
https://www.codechef.com/viewsolution/37287475
getting wrong answer

We should prevent sorting for counting elements as it takes n*log(n) time
Use map in c++ or dictionary in python ( O(n) time).

but not getting tle

You declared vector with size 10. I think thats the problem.

no I have change it
#include <bits/stdc++.h>
using namespace std;

int main() {
long long t;
cin>>t;
while(t–)
{
long long n=0;
cin>>n;

    int c1=0,c2=0,a1=0,b1=0;
 int x;
    vector<int> a(11,0);
   for(int i=0;i<n;i++)
  {
  cin>>x;
  a[x]++;
   }


    int c=*max_element(a.begin(), a.end());
    for(int i=0;i<=n;i++)
    {
        if(a[i]==c)
       { c1++;
       a1=a[i];}
        else
        if(a[i]==c-1)
       { c2++;
       b1=a[i];}
    }
    if(a1>b1&&c1>c2)
    cout<<a1<<endl;
    else
    cout<<b1<<endl;
}

// your code goes here
return 0;

}
???

please share solution link