1
#include <stdio.h>
int final[3];
int checkprime(int n,int loopcount)
{
    int flag=0,m;
    for(m=2; m<=loopcount; m++)
    {
        if(n%m==0 && n!=2)
        {
            flag=1;
            return 0;
            break;

        }
        else if(n==2)
        {
            flag=0;
            break;

        }
    }
    if(flag==0)
    {
        return 1;
    }
}
int main()
{
    int test_no,n,i,j,k,m,max=1,loopcount,product=1,max_available=0,count=0;
    scanf(" %d",&test_no);
    for(i=0; i<3; i++)
    {

        scanf(" %d",&n);
        int array[n];
        for(j=0; j<n; j++)
        {
            scanf(" %d",&array[n]);
            loopcount=array[n]/2;
            if(max<loopcount)
            {
                max=loopcount;
            }
        }
        loopcount=max;
        max=array[0];
        for(j=0; j<n; j++)
        {
            int x=checkprime(array[j],loopcount);
            if(x==1)
            {
                if(array[j]>=max)
                {
                    max=array[j];
                }
            }
        }
        product=product*max;
        max=1;
        for(j=0; j<n; j++)
        {
            int x=checkprime(array[j],loopcount);
            if(x==1)
            {
                if(array[j]>max && product!=array[j])
                {
                    max=array[j];
                    max_available=1;
                }
                else if(array[j]>=max && product==array[j] && max_available==0)
                {
                    max=product;
                }
            }
            if(x==0)
            {
                count++;
            }
        }
        if(count==n || count==n-1)
        {
            final[i]=-1;

        }
        else
        {
            product=product*max;
            final[i]=product;

        }
        product=1;
    }
    for(i=0; i<3; i++)
    {
        printf("%d\n",final[i]);
    }
    return 0;
}

The above code is not working properly ,I am unable to get the reason behind this , I am finding prime number two times and I have used a variable loopcount so as to find the number of iterations for checking whether the number is prime or not .

I have initially taken an array of size 3 and I am providing 3 inputs with each input of different array size and then I iterate twice for finding the maximum product , If I don't find any prime number , I output -1 on the output screen . Th first line depicts the total number of inputs which can be viewed as the number of test cases and for each test case , the first line depicts the size of array and the second line depicts the elements present in the array .Please help me to identify the problem , for the first input it is printing -1 but with some issues , Ideally , it should only go to if part

if(count==n || count==n-1)
{
    final[i]=-1;         
}

but it is going to code snippet of else part ,

else
{
    product=product*max;
    final[i]=product;
}

This I checked by writing printf statements in the else part , so I am able to get the value printed for the first input ,this is quite confusing since when if part is getting executed then why is the else part getting executed ?And for the rest inputs , it is printing garbage values .

Here is a input set:

3

5
1 4 6 8 10

3
2 2 9

2
156 13 

The corresponding output is:

-1
4
169
jbsu32
  • 1,036
  • 1
  • 11
  • 31
user123
  • 271
  • 1
  • 10
  • 4
    your code would be easier to read if it used consistent indentation – M.M Sep 01 '16 at 04:20
  • 1
    A side note: checking if the number is prime is an expensive operation, so you should twist checking: first you should check if the candidate number is greater than any of two already picked numbers and then check if it is prime or not. – Serge Sep 01 '16 at 04:32
  • Can you provide the corresponding output?? – jbsu32 Sep 01 '16 at 04:33
  • You might want to sort the array first, and check for primes starting at the high end. That way you can stop checking when you have found two primes. – rossum Sep 08 '16 at 14:58

2 Answers2

2

You should scan array[j] instead of array[n]. nth index is not valid.

int array[n];
for(j=0; j<n; j++)
{
    scanf(" %d",&array[n]);  /// Problem is here
    loopcount=array[n]/2;
    if(max<loopcount)
    {
        max=loopcount;
    }
}
jbsu32
  • 1,036
  • 1
  • 11
  • 31
  • I corrected my code and I am getting correct values for only 2nd input now , I have modified the code at lines , if((count==n||count==n-1)&& max_available==0) and after assigning product value to final array , I again re-assign max_available=0 ,please check the modified code , I am unable to trace the exact issue , It is printing 1 for the first as well as 3rd input https://ideone.com/QK8jre – user123 Sep 01 '16 at 07:10
  • Ok. Will you please edit your question and provide corresponding output for the input set?? As the problem is unclear from you question. – jbsu32 Sep 01 '16 at 07:13
  • The output for the corresponding input should be -1 , 4 and 169 , for the first input I have no prime number , so output is -1 , for the 2nd input , I have only one prime number , so that same number is multiplied with itself and hence output is 4 , for the 3rd input also I have only 1 prime number , so output is 169 but I am not getting the desired output – user123 Sep 01 '16 at 08:24
  • So, if input set is `2 5 4 7` then output is `35` ?? – jbsu32 Sep 01 '16 at 08:42
  • Yes sir , product of 5 and 7 – user123 Sep 01 '16 at 10:32
1

Previously, you had a far complex idea. Here is some optimizations done to your code. Feel free to ask anything. Hope it helps :)

#include <stdio.h>
#include <math.h>

int checkprime(int n)
{
    int flag=0,m;
    for(m=2; m<=sqrt(n); m++)
    {
        if(n%m==0)return 0;
    }
    return 1;
}
int main()
{
    int test_no,n,i,j,k,m,max=1,loopcount,product=1,max_available=0,count=0;
    scanf("%d",&test_no);
    for(i=0; i<test_no; i++)
    {
        scanf(" %d",&n);
        int array[n];
        for(j=0; j<n; j++)
        {
            scanf("%d",&array[j]);
        }
        sort(array);  // Use any sort algorithm you wish to sort the array
        int _1=1, _2=1;
        for(int j=n-1; j>=0; j--){
            if(checkprime(array[j])==1){
                if(_1==1){
                    _1=array[j];
                }
                else if(_1!=1 && _2==1){
                    _2=array[j];
                    break;
                }
            }
        }
        if(_1==1 && _2==1)printf("-1\n");
        else if(_2==1 && _1!=1)printf("%d\n", _1*_1);
        else printf("%d\n", _1*_2);
    }
    return 0;
}
jbsu32
  • 1,036
  • 1
  • 11
  • 31