#include<stdio.h>
int main(void)
{
int i,t,m,max,j,N;
scanf("%d",&t);
while(t–)
{ int b[1000]={0};
j=0;
max=0;
scanf("%d",&N);
for(i=0;i<N;i++)
{
scanf("%d",&m);
b[m]+=1;
}
for(i=0;i<=1001;i++)
{
//printf("%d\t",b[i]);
if(max<b[i])
{
max=b[i];
j=i;
}
}
printf("%d %d\n",j,max);
}
return 0;
}
You may also view the solution at the link below:
http://www.codechef.com/viewsolution/6068287
Thanx for your support
Your approach is correct (there are other solutions strategies also ). But you are not taking care of the constraints values.
1 <= T <= 100, 1 <= N <= 100
and for all i in [1…N] : 1 <= A[i] <= 10000
The values of T and N are handled correctly but A[i] can have maximum value of 10000.
Your array b is suffering from array index out of bounds runtime error for i >=1000, you need to make the below mentioned adjustments for all i in [1…N] : 1 <= A[i] <= 10000.
#include"stdio.h"
int main(void) {
int i,t,m,max,j,N;
scanf("%d",&t);
while(t--)
{
int b[10001]={0}; //change here, increase the size of the array
j=0;
max=0;
scanf("%d",&N);
for(i=0;i<N;i++)
{
scanf("%d",&m);
b[m]+=1;
}
for(i=0;i<10001;i++) //change here, as the size is increased you need to adjust the loop accordingly
{
//printf("%d\t",b[i]);
if(max<b[i])
{
max=b[i];
j=i;
}
}
printf("%d %d\n",j,b[j]);
}
return 0;
}
Your approach with these modification is accepted, see it here.
P.S. : Your approach can be modified to require less memory ( you are allocating array size of 10001).
Thanks.
A silly mistake of mine!!