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[50];
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[i];
if(arr[i]<min1)
{
min2=min1;
min1=arr[i];
}
else if(arr[i]<min2 && arr[i]!=min1)
min2=arr[i];
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[i]%min2));
// if yes,divide all nos by gcd.
if(i==n)
{
for(i=0;i<n;i++)
arr[i]/=min2;
}
}
//print result
for(i=0;i<n;i++)
cout<<arr[i]<<" ";
cout<<"\n";
}
return 0;
}