GCD and LCM Euclidean Algorithm in DSA C++ - Time & Space Complexity
We want to understand how fast the Euclidean algorithm finds the greatest common divisor (GCD) of two numbers.
How does the number of steps change as the input numbers get bigger?
Analyze the time complexity of the following code snippet.
int gcd(int a, int b) {
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
int lcm(int a, int b) {
return (a / gcd(a, b)) * b;
}
This code finds the GCD of two numbers using the Euclidean algorithm and then calculates their LCM using the GCD.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The while loop repeatedly calculates the remainder of division (a % b).
- How many times: The loop runs until the second number becomes zero, which depends on the size of the inputs.
Each step reduces the size of the numbers significantly, so the number of steps grows slowly even if inputs get large.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 4-5 steps |
| 100 | About 7-8 steps |
| 1000 | About 10-12 steps |
Pattern observation: The number of steps grows roughly proportional to the number of digits, not the number itself.
Time Complexity: O(log(min(a, b)))
This means the steps grow slowly, roughly with the number of digits in the smaller input number.
[X] Wrong: "The Euclidean algorithm takes as many steps as the size of the input numbers."
[OK] Correct: Actually, each step reduces the problem size quickly, so the number of steps grows with the number of digits, not the number itself.
Understanding this algorithm's time complexity shows you can analyze efficient number operations, a useful skill in many coding problems.
"What if we used subtraction instead of modulo in the GCD calculation? How would the time complexity change?"
Practice
a and b?Solution
Step 1: Understand the Euclidean algorithm purpose
The Euclidean algorithm is designed to find the greatest common divisor (GCD) of two integers by repeatedly applying the remainder operation.Step 2: Differentiate GCD from LCM
While GCD finds the largest number dividing both, LCM finds the smallest number divisible by both. The Euclidean algorithm is for GCD.Final Answer:
The Greatest Common Divisor (GCD) -> Option DQuick Check:
Euclidean algorithm = GCD [OK]
- Confusing GCD with LCM
- Thinking it calculates sum or difference
- Assuming it finds prime factors
Solution
Step 1: Identify the base case in Euclidean recursion
The base case occurs when the second numberbbecomes zero, then the GCD is the first numbera.Step 2: Match the correct syntax
The correct syntax isif (b == 0) return a;which stops recursion and returns the GCD.Final Answer:
if (b == 0) return a; -> Option CQuick Check:
Base case: b == 0 returns a [OK]
- Returning 0 instead of a
- Checking if a == b instead of b == 0
- Swapping a and b incorrectly
int gcd(int a, int b) {
if (b == 0) return a;
return gcd(b, a % b);
}
int main() {
std::cout << gcd(48, 18);
return 0;
}Solution
Step 1: Apply Euclidean algorithm steps for gcd(48, 18)
Calculate gcd(48, 18): 48 % 18 = 12, so next call gcd(18, 12). Then gcd(18, 12): 18 % 12 = 6, next gcd(12, 6). Then gcd(12, 6): 12 % 6 = 0, next gcd(6, 0).Step 2: Reach base case and return result
When b=0, return a=6. So gcd(48, 18) = 6.Final Answer:
6 -> Option AQuick Check:
gcd(48,18) = 6 [OK]
- Returning 12 instead of 6
- Confusing modulo operation
- Stopping recursion too early
int gcd(int a, int b) {
if (a == 0) return b;
return gcd(a % b, b);
}Solution
Step 1: Check base case correctness
The base caseif (a == 0) return b;is valid because when a is zero, gcd is b.Step 2: Analyze recursive call parameters
The recursive call usesgcd(a % b, b), but Euclidean algorithm requiresgcd(b, a % b). The parameters are swapped incorrectly.Final Answer:
The recursive call parameters are swapped incorrectly -> Option BQuick Check:
Correct recursion order: gcd(b, a % b) [OK]
- Swapping parameters in recursive call
- Confusing base case condition
- Using modulo with wrong variable
Solution
Step 1: Recall the relationship between LCM and GCD
LCM(a, b) * GCD(a, b) = a * b. So LCM = (a * b) / GCD(a, b).Step 2: Apply formula for 15 and 20
Calculate gcd(15, 20) = 5, then LCM = (15 * 20) / 5 = 60.Final Answer:
LCM = (15 * 20) / gcd(15, 20); -> Option AQuick Check:
LCM * GCD = 15 * 20 [OK]
- Dividing GCD by product instead of product by GCD
- Adding numbers instead of multiplying
- Multiplying GCD by sum instead of dividing
Ready to prove it?
Pass the test and this pattern moves unknown
