Why is my code for the VOTERS problem giving WA???

submission

You last submit is returning WA, read FAQ.

Statements like this one are problem:

printf("\n ENTER THE NUMBER OF VOTERS IN THE THREE LISTS <SEPARATED BY WHITESPACES>:");

Part from FAQ

If your program starts by printing ‘Enter the number’ and the problem does not tell you to do so, then since this is not part of the correct output, you will be never be judged correct regardless of what the rest of your program does.

Another issue that will cause problems is your use of malloc:

When initialising p, you call malloc to allocate (n1+n2+n3) bytes, and use that as (n1+n2+n3) integers. Try allocating (n1+n2+n3) * sizeof(int) bytes, or (probably better) create a global array that can hold n1+n2+n3 integers for the largest possible test case.

Similarly, when initialising q, you allocate enough bytes to store one int, but you use it as an array of ints of a larger size (k is incremented, an upper limit is not clear to me). If you have an upper limit, use that.

@u_n_o: try to generate test file

50000 50000 50000
10
20
...
500000
10
20
...
500000
10
20
...
500000

result should be again

10
20
...
500000
1 Like

CodeChef: Practical coding for everyone I have removed the unnecessary print statements but still its giving WA. I have checked my program with the sample test case and it works just fine…I dont seem to figure out the problem… :confused: @betlista

Maybe the code should be:

printf("%d\n",k);
for(i=0;i<k;i++)
{
printf("%d\n",q[i]);
}

#include<stdio.h>
#include<stdlib.h>
int main()
{
int p[50000],q[50000];
int n1=0,n2=0,n3=0,i,j,k=0,ctr=1,piv=0;
scanf("%d %d %d",&n1,&n2,&n3);
for(i=0;i<(n1+n2+n3);i++)
{
scanf("%d",&p[i]);
}
for(i=0;i<(n1+n2+n3);i++)
{
piv=p[i];
if(piv==0)
continue;
ctr=1;
for(j=i+1;j<(n1+n2+n3);j++)
{
if(piv==p[j])
{
p[j]=0;
ctr++;
}
}
if(ctr>1)
{
q[k]=piv;
k++;
}
}
for(i=0;i<k;i++)
{
for(j=i+1;j<k;j++)
{
if(q[i]>q[j])
{
piv=q[j];
q[j]=q[i];
q[i]=piv;
}
}
}
printf("\n%d",k);
for(i=0;i<k;i++)
{
printf("\n%d",q[i]);
}
return 0;
}

I’ve modified the code and it works fine with the sample test case provided…but still am getting a WA…I cant seem to figure out what goes wrong when it runs on the judge…

yeah the code is correct…somehow the slashes got erased in the comment… :stuck_out_tongue:

In your code the \n is before the numbers. It should be after the numbers.