AfterAcademy Tech
•
10 Feb 2020

Difficulty: Medium
Asked in: Google
Given two integers say A and B, you have to find the greatest common divisor or highest common factor of the given integers.
For example: INPUT: 10 15
OUTPUT: 5
INPUT: 36 48
OUTPUT: 12
Explanation: 5 is the largest integer that is factor of both 10 and 15. Similarly, 12 is the largest integer that divides both 36 and 48 completely.
Possible follow-up questions to ask: →
We are going to see two solutions to find the GCD or HCF of two numbers →
Solution idea
The simple idea here is to find the factors of both the given numbers and then multiply the common factors to get the GCD.
Example: →
Factors of number 30: 2x3x5
Factors of number 20: 2x2x5
Since the common factors are 2x5, the GCD will be 2x5 = 10
Solution steps
Pseudo-Code
int getGCD(int number1, int number2)
{
int result = 1
for(int i=2 to min(number1,number2))
{
if( i % number1 == 0 and i % number2 == 0 )
result=i
}
return result
}
Complexity Analysis
Time Complexity: O(min(number1,number2)), that is the minimum among the two integers(Why?)
Space Complexity: O(1)
We will see more efficient solution using euclidean algorithm for GCD.
Solution idea
Euclidean Algorithm is based on the principle that the GCD of two numbers remains the same if the larger of the two numbers is replaced by its difference with the smaller number. This process keeps reducing the larger number until the number becomes equal to the smaller number. When this happens, we get the GCD.
Solution steps
Pseudo-Code
int getGCD(int number1, int number2)
{
while(number1 != number2)
{
if( number1 > number2 )
number1 = number1 - number2
else
number2 = number2 - number1
}
return number1
}
Complexity Analysis
Time Complexity: O(max(number1, number2))
(Why? Hint: Consider the case when there is a large difference between the numbers)
Space Complexity: O(1)
★ Can you think of optimizing the Euclidean Algorithm? Can we replace the subtraction with some more efficient operators?
Solution idea
The problem with the above algorithm is the repetitive calculation(subtraction) in the case when one of the numbers is very large. To avoid this, we can change the subtraction operation with division. The algorithm will end once zero remainder is found.
Solution steps
Pseudo-code
int getGCD(int number1, number2)
{
if( number2 == 0 )
return number1
return getGCD(number2, number1%number2)
}
Complexity Analysis
Time Complexity: O(log(number1+number2))(Why?)
Space Complexity: Think! ( Hint: What is the recursive stack space used?)
Critical ideas to think!

Happy Coding! Enjoy Algorithms!!
AfterAcademy Tech
This is an interview problem asked in companies like Google, Microsoft, Amazon. The problem is to remove duplicates from a sorted array or list.

AfterAcademy Tech
This is an interview problem asked in companies like Amazon, Microsoft. The problem is to merge two binary trees. The overlapping nodes are replaced by the sum of corresponding nodes. We are discussing two ways of solving this.

AfterAcademy Tech
This is an interview problem asked in Google technical interview. Given a BST(Binary Search Tree) with non-negative values, write a program to find the minimum absolute difference between values of any two nodes.

AfterAcademy Tech
This is an interview problem asked in companies like Microsoft, Amazon, Adobe, Flipkart, etc. Here, we need to implement Queue data structure using another data structure Stack. This problem is meant for testing the data structure knowledge.
