Recipe beginner problem
when i tested all limits it worked fine, codechef showed it gives wrong answer. probably am missing any bound, can anyone help me?

for(int i=0;i<NumberOfIngredients;i++){

This logic fails for cases like-

4 10 12 14 16

Here the reduced form is 5 6 7 8 but none of numbers are divisible by min. What you should do is, check if each number is divisible by 2, if yes, keep dividing by 2 till they are not divisible by 2 any more. then check for 3 and so on.


Try using prime factorisation. Storing the prime factors will let you reduce them to lowest terms easily. You only need factors and their count which are common to all elements.

1 Like

You are making the code bit complicated than necessary. To reduced to minimum possible recipe items, all you need to do is find gcd (because you can divide each item only by the number of times it is common to each item, which in other words implying to the gcd value. Any other number would give you a fraction item) and then divide each item by gcd.

Below is my quick code:

using namespace std;

int main(){
	int t;
		int n;
		int a[n], d = 0;
		for(int i=0;i<n;i++){
			d = __gcd(d,a[i]);
		for(int i=0;i<n;i++) cout<<a[i]/d<<" ";
	return 0;

you have to find gcd of an array. complexity is the key.

thanks for clarifying things :slight_smile:

1 Like

You’re welcome dear :slight_smile: