I simply used arrays to count the frequency of each number....My
accepted solution is:
#include<stdio.h>
int main()
{
int t,n,i,p,x,chk,k;
int pos[1000];
int neg[1000];
int nos[1000000];
scanf("%d",&t);
while(t--)
{
chk=0;
p=0;
n=0;
scanf("%d",&n);
for(i=0;i<1000;i++)
{
pos[i]=0;
neg[i]=0;
}
for(i=0;i<n;i++)
{
scanf("%d",&x);
nos[i]=x;
if(x>=0)
{
pos[x]++;
}
else
{
neg[-1*x]++;
}
}
for(i=0;i<n;i++)
{
if(nos[i]>=0)
{
if(pos[nos[i]]>n/2)
{
k=nos[i];
chk=1;
}
}
else
{
if(pos[-1*nos[i]]>n/2)
{
k=nos[i];
chk=1;
}
}
}
if(chk==0)
{
printf("NO\n");
}
else
{
printf("YES %d\n",k);
}
}
return 0;
}
--
You received this message because you are subscribed to the Google Groups
"Algorithm Geeks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/algogeeks?hl=en.
