JavaEasyMath10 min
Find the GCD and LCM
Print the nonnegative GCD and LCM separated by a space; define both as 0 if both inputs are 0. Follow the stated ordering and edge-case conventions exactly. Interviewers use this task to probe algorithm selection, invariants, and boundary handling.
Input
Two integers a and b.
Output
Print the nonnegative GCD and LCM separated by a space; define both as 0 if both inputs are 0.
Constraints
-1000000000 <= a,b <= 1000000000.
Example
Input
12 18
Output
6 36
Example 2 input
999999937 1000000000
Output
1 999999937000000000
This example exercises large coprime inputs.
Public test cases (2)
Input
12 18
Expected output
6 36
Input
-8 12
Expected output
4 24
Hints (2)
- Identify the state needed to avoid recomputing work for each element.
- Apply Euclid's remainder algorithm, then divide before multiplying to compute the LCM.
Target complexity: time O(log(max(|a|,|b|))) · space O(1)
