Cutting Recipes
Practice this coding question on Coding Gurukul.
Problem statement
A cooking club wants to reduce a recipe to the smallest whole-number ingredient quantities while preserving its exact proportions. The original quantities are all positive integers. Staff can divide every quantity by the same positive integer only when no ingredient becomes fractional. Your program finds the fully reduced recipe for each batch of ingredient lists. Your task For each test case, divide every quantity by the greatest common divisor of all quantities. Output the reduced quantities in their original order. The resulting list must have no common integer divisor greater than one, so no further whole-number reduction is possible. Important details Do not sort the ingredients, since each position refers to a different ingredient. Reducing each value by a separate divisor would change the recipe. With one ingredient, the reduced quantity is always 1. Worked example For quantities [12,18,30], the common divisor is 6. Dividing every quantity by 6 gives [2,3,5], preserving the proportions.
Input format
First line: T. For each test case: n followed by n positive quantities.
Output format
Print the reduced quantities on one line per test case.
Constraints
1 <= T <= 100; 1 <= n <= 1000; 1 <= quantity <= 1000000000.
Sample input
1 3 12 18 30
Sample output
2 3 5
Sign in to run or submit a solution in C++, Java or Python.