✏️ Explanatory Question
Question:
Finding the greatest common divisor (GCD) of two positive integers results in the time complexity of O(log N), where N is the larger number of two inputs. Justify the statement.
Question:
Finding the greatest common divisor (GCD) of two positive integers results in the time complexity of O(log N), where N is the larger number of two inputs. Justify the statement.
Answer:
The statement is correct. The time complexity of finding GCD is O(log N) because it uses the Euclidean Algorithm, which repeatedly reduces the problem size.
Explanation:
The Euclidean Algorithm works as follows:
gcd(a, b) = gcd(b, a % b)
In each step:
This reduction happens logarithmically. That means:
For example:
gcd(48, 18)
→ gcd(18, 12)
→ gcd(12, 6)
→ gcd(6, 0)
Here, the numbers reduce quickly in very few steps.
Conclusion: