Wrong answer in MAXCOUNT problem. Please help!!

I have gone through many test cases,yet I think I might be missing some special case or am making some silly mistake. Can anyone please help me out with the wrong answer…??
Here is my code in C:-

#include<stdio.h>
int main()
{
long int t,n,max=0,k,l,i,nummax=0,num;
scanf("%ld",&t);
if (t < 1 || t > 100)
    return -1;
while(t--)
{
    scanf("%ld",&n);
  //  nummax=0,max=0,k=0,l=0,i=1;
    if (n < 1 || n > 100)
        return -1;
    long int arr[10000]={0};
    for(i=1;i<=n;i++)
    {
        scanf("%ld",&num);
        if (num < 1 && num > 10000)
            return -1;
        if (num>nummax)
            nummax=num;
        arr[num]=arr[num]+1;
    }
    /*for(i=1;i<=n;i++)
        printf("%ld",arr[i]);
    */
    max=arr[1];
    k=1;
    for(i=1;i<=nummax;i++)
    {
        if (arr[i+1]>max)
        {
            max=arr[i+1];
            k=i+1;
        }
        else if (arr[i+1]==max)
        {
            max=arr[i+1];
            l=i+1;
        }
    }
    //printf("%ld %ld\n",k,l);
    if (arr[k]==max && arr[l]==max)
    {
        if (k>l)
            printf("%ld %ld\n",l,arr[l]);
        else
            printf("%ld %ld\n",k,arr[k]);
    }
    else
        printf("%ld %ld\n",k,arr[k]);
}
return 0;
}

Declare array of 10002 and it will work :slight_smile:
Take care of corner cases.

You are declaring array of 10000 and so you can index it from 0 to 9999 only and not 10000. <-- First Mistake

Secondly, while calculating max, you are looping from 1 to nummax, so if nummax=10000, then at last comparision , you will check if(a[10000+1]>max) i.e. a[10001] which is again out of bound.

So either improve your consciousness for such cases or declare it of size 10002 to get it work (or >10002 to be extra safe which is not needed)

1 Like