You are not logged in. Please login at www.codechef.com to post your questions!

×

CUTTING RECIPES

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;

}

asked 03 Aug '14, 15:49

s_entrepreneur's gravatar image

0★s_entrepreneur
11
accept rate: 0%


@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.

link

answered 04 Aug '14, 02:35

ironmandhruv's gravatar image

4★ironmandhruv
333239
accept rate: 20%

toggle preview
Preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported
  • mathemetical formulas in Latex between $ symbol

Question tags:

×3,677
×1,887
×28

question asked: 03 Aug '14, 15:49

question was seen: 1,245 times

last updated: 04 Aug '14, 02:35