problem regarding chef and equality

As the answer of the “chef and equality” problem i wrote this code. It is giving correct answer,stil wrong answer is being shown…why?

#include <iostream>
#include <algorithm>
using namespace std;
 
int main()
{
   int t,i,j,s1=0,maxi=0;
   cin>>t;
   while(t--)
   {
       int n;
       cin>>n;
       int a[n];
       for(i=0;i<n;i++)
       {
           cin>>a[i];
       }
       sort(a,a+n);i=0;
       while(i<n)
       {
           j=i+1;s1=1;
           while(a[i]==a[j])
           {
               s1++;
               j++;
           }
           if(s1>maxi){maxi=s1;}
           i=j;
       }
       cout<<n-maxi<<endl;
   }
   return 0;
}

The problem is that you are not setting the value of maxi to 0 in every test case. So in the subsequent test cases, the previous value of maxi is used, giving a WA.

Just put the following line inside the while(t--) loop, and you should get AC.

maxi = 0
1 Like

As a habit, try to include problem link as well, or just give a link to your submitted solution. Theres no feature of “searching” problem on codechef as far as i know.

1 Like