![]() |
VOOZH | about |
Given three integers x, y, z, the task is to compute the value of GCD(LCM(x,y), LCM(x,z)) where, GCD = Greatest Common Divisor, LCM = Least Common Multiple
Examples:
Input: x = 15, y = 20, z = 100
Output: 60
Explanation: The GCD of 15 and 20 is 5, and the LCM of 15 and 20 is 60, which is then multiplied by the given z (100) to illustrate the distributive property in LCM and GCD operations
Input: x = 30, y = 40, z = 400
Output: 120
Explanation: The GCD of 30 and 40 is 10, and the LCM of 30 and 40 is 120, which is then multiplied by the given z (400) to show the distributive property in combining GCD and LCM operations
One way to solve it is by finding GCD(x, y), and using it we find LCM(x, y). Similarly, we find LCM(x, z) and then we finally find the GCD of the obtained results.
An efficient approach can be done by the fact that the following version of distributivity holds true:
GCD(LCM (x, y), LCM (x, z)) = LCM(x, GCD(y, z))
For example, GCD(LCM(3, 4), LCM(3, 10)) = LCM(3, GCD(4, 10)) = LCM(3, 2) = 6
This reduces our work to compute the given problem statement.
120
Time Complexity: O(log(min(a,b))
Auxiliary Space: O(log(min(a,b))
As a side note, vice versa is also true, i.e., gcd(x, lcm(y, z)) = lcm(gcd(x, y), gcd(x, z)
Please refer Properties of GCD for details.