< Back to forum

correct answer but giving wrong ans in codechef...CUTTING RECEIPE of codechef... link https://www.codechef.com/viewsolution/17287721

#include<stdio.h>

int main()
{
int t,i,n,s,z,flag;
scanf("%d",&t);
while(t--)
{

 scanf("%d",&n);
 int arr[n];

 for(i=0;i<n;i++)
 {
  scanf("%d",&arr[i]);
  if(i==0)
    s=arr[i];

  if(arr[i]<=s)
        s=arr[i];
 }

  flag=0;

  for(i=0;i<n;i++)
   {
     if(arr[i]%s!=0)
        flag++;

   }

if(flag==0)
{
 for(i=0;i<n;i++)
   {

    arr[i]=arr[i]/s;
    printf(" %d",arr[i]);


   }
}

else
{

     for(i=0;i<n;i++)
   {

    printf(" %d",arr[i]);


   }
}

   printf("\n");

}


return 0;
}

 

Asked by: Manish_Kumar_Savita on April 7, 2019, 6:34 p.m. Last updated on April 7, 2019, 6:34 p.m.


Enter your answer details below:


Enter your comment details below:




1 Answer(s)

avatar

In your solution, you checking if all the elements of the array are divisible by the smallest array element or not. Your code will give correct output if GCD (greatest common divisor) of all the array elements is the smallest array itself or 1.

But let's check this test case where GCD is neither the smallest element in the array nor 1.

N = 3
Array = {20, 50, 25}

Your output: 20 50 25
​​​​​​​Expected Output: 4 10 5

Also, please print your answer as left alligned.
You are using:

printf(" %d", arr[i]);

Correct way to print the answer should be:

printf("%d ", arr[i]);

 

Siddharth_Garg last updated on April 7, 2019, 6:34 p.m. 0    Reply    Upvote   

Instruction to write good question
  1. 1. Write a title that summarizes the specific problem
  2. 2. Pretend you're talking to a busy colleague
  3. 3. Spelling, grammar and punctuation are important!

Bad: C# Math Confusion
Good: Why does using float instead of int give me different results when all of my inputs are integers?
Bad: [php] session doubt
Good: How can I redirect users to different pages based on session data in PHP?
Bad: android if else problems
Good: Why does str == "value" evaluate to false when str is set to "value"?

Refer to Stack Overflow guide on asking a good question.