 # CUTTING RECIPES

#1

Problem: http://www.codechef.com/problems/RECIPE
difficulty:easy
Hello,
I have tried many times but i am unable to point out any errors (logical) errors in my code.I am getting a wrong answer for this code.It works fine on my machine giving correct output.Can anybody help me out.
I have found the two smallest numbers(min1 and min2) and then their gcd.
If the gcd is not 1 then i am checking whether their gcd divides all the remaining nos without leaving behind any remainder.If it is 1 i simply print all nos.
If so then i divide all the numbers by the gcd and output the result.
Else i simply output all the numbers.

``````#include<iostream>
``````

using namespace std;

int main()
{
int t,n,i,min1,min2,arr;
cin>>t;
while(t–)
{

``````    cin>>n;
i=0;
//since max value of any no. can be 1000
min1=min2=1001;
//min1<min2
while(i<n)
{
cin>>arr*;
if(arr*<min1)
{
min2=min1;
min1=arr*;
}
else if(arr*<min2 && arr*!=min1)
min2=arr*;
i++;
}
//if all nos. are same
if(min2==1001)
min2=min1;
//find gcd of min1 and min2
else
{
while(min1)
{
i=min1;
min1=min2%min1;
min2=i;
}
}
i=-1;
if(min2!=1)
{
//check whether gcd divides all nos.
while(++i<n && !(arr*%min2));
//  if yes,divide all nos by gcd.
if(i==n)
{
for(i=0;i<n;i++)
arr*/=min2;
}
}
//print result
for(i=0;i<n;i++)
cout<<arr*<<" ";
cout<<"
``````

";
}
return 0;
}

#2

@s_enterpreneur

Consider the array : 8 16 22

Minimum(m1)=8 and second minimum(m2)=16.

gcd(m1,m2)=gcd(8,16)=8 which is non-zero.

=>your output is 1 2 2 (note that 22/8 =2)

However required output is 4 8 11.

Hope you understand.