1 / 8
Dec 2014

my code is ideone.com/9alSLp
i have tested all the test cases in question as well as in various posts but still getting wa. plz help or give a tricky case for which the code fails.

  • created

    Dec '14
  • last reply

    Dec '14
  • 7

    replies

  • 561

    views

  • 2

    users

  • 1

    link

I can't see ideone code. Please post your code inline using code tags.

#include<stdio.h>
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n,i,j,l;
        scanf("%d",&n);
        int a[n];
        for(i=0;i<n;i++)
            scanf("%d",&a[i]);
        int f;
        int b[100],c[100];
        int k=0;
        for(i=1;i<=100;i++)
            {
                 f=0;
                for(j=0;j<n;j++)
                {
                    if(a[j]==i)
                        f++;
                }
            c[k]= f;
            if(f!=0 && f%i==0)
            {
                b[k]= i;
            }
            else
                b[k]= 0;
            k++;
        }
    int p=0;
    for(l=0;l<100;l++)
    {
        if(b[l]>0)
        {
            p++;
            break;
        }
    }
    if(p==0)
        printf("-1\n");
    else
    {
    int max=0;
    for(l=0;l<100;l++)
    {
    if(c[l]== 0)
            continue;
    else if(c[l]>max)
        max = c[l];
    }
    int min=101;
    for(l=0;l<100;l++)
    {

            if(b[l]== 0)
             continue;
            if(max==c[l] && max%b[l]==0)
                {
                if(b[l]<min)
                    min= b[l];
                }
    }
    if(min ==101)
        min= -1;
    printf("%d\n",min);
    }
}
return 0;
}

You need to think your algorithm through carefully instead of just throwing code around.

int b[100],c[100];

The values of b are uninitialized at this point. They might be zero, they might be 10000.

        int p=0;
        for(l=0;l<100;l++)
        {
            if(b[l]>0)
            {
                p++;
                break;
            }
        }

Since the values of b are uninitialized, if k is not equal to 100 your value of p is unreliable.

        if(p==0)
            printf("-1\n");
        else

Since your value of p is no reliable there are likely many instances where you should print -1 that you do not.

#include<stdio.h>
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int n,i,j,l;
        scanf("%d",&n);
        int a[n];
        for(i=0;i<n;i++)
            scanf("%d",&a[i]);
        int f;
        int b[100],c[100];
        for(i=0;i<100;i++)
            b[i]=0;
        int k=0;
        for(i=1;i<=100;i++)
            {
                 f=0;
                for(j=0;j<n;j++)
                {
                    if(a[j]==i)
                        f++;
                }
            c[k]= f;
            if(f!=0 && f%i==0)
            {
                b[k]= i;
            }
            else
                b[k]= 0;
            k++;
        }
    int p=0;
    for(l=0;l<100;l++)
    {
        if(b[l]>0)
        {
            p++;
            break;
        }
    }
    if(p==0)
        printf("-1\n");
    else
    {
    int max=0;
    for(l=0;l<100;l++)
    {
    if(c[l]== 0)
            continue;
    else if(c[l]>max)
        max = c[l];
    }
    int min=101;
    for(l=0;l<100;l++)
    {

            if(b[l]== 0)
             continue;
            if(c[l]==max && max%b[l]==0)
                {
                if(b[l]<min)
                    min= b[l];
                }
    }
    if(min ==101)
        min= -1;
    printf("%d\n",min);
    }
}
return 0;
}

You're returning -1 far too many times when there is a valid solution.

			int max=0;
			for(l=0;l<100;l++)
			{
				if(c[l]== 0)
					continue;
				else if(c[l]>max)
					max = c[l];
			}

Suggested Topics

Topic Category Replies Views Activity
C and C++ 0 21 13d

Want to read more? Browse other topics in C and C++ or view latest topics.