Count of maximum

why is the following showing wrong answer on codechef though it gives the correct answer for the test cases provided and several other cases that i fed into the program.
Here is the question:

and here is the code

#include<iostream>
#include<stdio.h>
#include<string.h>
int main()
{
    int t,n,a,num[10001],freq,val;
    scanf("%d",&t);
    while(t--)
    {
        memset(num,0,10001);
        scanf("%d",&n);
        freq=0;
        val=0;
        for(int i=0;i<n;i++)
        {
            scanf("%d",&a);
            num[a]++;
            if(freq<num[a])
            {
                freq=num[a];
                val=a;
            }
            else if(freq==num[a])
                val=min(val,a);
        }
        printf("%d %d\n",val,freq);
    }
}

You have neglected a small but very vital piece of information that is causing WA ::-> In case of ties, choose the smaller element first. .

I think you should be able to get AC on your own now that you know where the problem is.

2 Likes

@kcahdog i have made the change as u pointed out… but it is still giving wrong answer…
am i missing something else or have i not made the correction properly…

Your correction was right and code should have worked.But dunno why but i had the gut feeling that something was wrong with memset and voila! Here is the accepted code in which i have replaced memset by a simple for loop to initialize to zero : http://www.codechef.com/viewsolution/3267630. Dunno why memset is not functioning properly.

1 Like

Just change the memset function from

mem(num,0,10001) to mem(num,0,10001*sizeof(int))

and you will get AC.Hope this helps.

1 Like

hmm,
i guess i should read a bit about this memset function.
finally code accepted!!!

//