Cutting Recipes | CodeChef Solution

Hello coders, today we are going to solve Cutting Recipes CodeChef Solution whose Problem Code is RECIPE.

Cutting Recipes


The chef has a recipe he wishes to use for his guests, but the recipe will make far more food than he can serve to the guests. The chef therefore would like to make a reduced version of the recipe which has the same ratios of ingredients, but makes less food. The chef, however, does not like fractions. The original recipe contains only whole numbers of ingredients, and the chef wants the reduced recipe to only contain whole numbers of ingredients as well. Help the chef determine how much of each ingredient to use in order to make as little food as possible.

Input Format

Input will begin with an integer T, the number of test cases. Each test case consists of a single line. The line begins with a positive integer N, the number of ingredients. N integers follow, each indicating the quantity of a particular ingredient that is used.

Output Format

For each test case, output exactly N space-separated integers on a line, giving the quantity of each ingredient that the chef should use in order to make as little food as possible.

Sample Input

2 4 4
3 2 3 4
4 3 15 9 6

Sample Output

1 1
2 3 4
1 5 3 2


All ingredient quantities are between 1 and 1000, inclusive.

Solution – Cutting Recipes | CodeChef Solution


#include <iostream>
using namespace std;

int gcd (int a, int b)
    if(a == 0)
        return b;
    return gcd(b % a, a);

int gcdn (int arr[], int n)
    int res = arr[0];
    for(int i = 1; i < n; i++)
        res = gcd(arr[i], res);
        if (res == 1)
            return 1;
    return res;

int main() {
	// your code goes here
	int t;
	cin >> t;
	    int n, arr[50], i, fact;
	    cin >> n;
	    for (i = 0; i < n; i++)
	        cin >> arr[i];
	    fact = gcdn(arr, n);
	    for (i = 0; i < n; i++)
	        cout << arr[i] / fact << " ";
	    cout << endl;
	return 0;


#Solution Provided by CodingBroz
T = int(input())
for i in range(T):
    l = list(map(int,input().split()[1:]))
    n = min(l)
        if all([i % n == 0 for i in l]):
            print(*[i // n for i in l])
        n = n - 1


/* package codechef; // don't place package name! */

import java.util.*;
import java.lang.*;

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
     public int gcd(int a, int b)
        if (a == 0)
            return b;
        return gcd(b % a, a);

    // Function to find gcd of array of
    // numbers
    public int findGCD(int arr[], int n)
        int result = 0;
        for (int element: arr){
            result = gcd(result, element);

            if(result == 1)
                return 1;

        return result;

	public static void main (String[] args) throws java.lang.Exception
		 Scanner sc=new Scanner(;
       int n=sc.nextInt();
       for (int i=0;i<n;i++){
           int a=sc.nextInt();
           int []b=new int[a];
           int []c=new int[a];
           for (int j=0;j<a;j++){
               b[j]= sc.nextInt();
           Codechef df=new Codechef();
           int result=b[0];
           for (int j=1;j<a;j++){
           for (int j=0;j<a;j++){
             //  System.out.println(b[0]);
               System.out.print(b[j]/result+" ");


Disclaimer: The above Problem (Cutting Recipes) is generated by Hacker Rank but the Solution is Provided by CodingBroz. This tutorial is only for Educational and Learning Purpose.

Leave a Comment

Your email address will not be published.