public class Solution {
public static int calcGCD(int n, int m){
while(m>0 && n>0){
if(m>n){
m=m%n;
}
else{
n=n%m;
}
}
if(m==0){
return n;
}
else return m;
}
}
Problem of the day
You are given two integers 'n', and 'm'.
Calculate 'gcd(n,m)', without using library functions.
The greatest common divisor (gcd) of two numbers 'n' and 'm' is the largest positive number that divides both 'n' and 'm' without leaving a remainder.
Input: 'n' = 6, 'm' = 4
Output: 2
Explanation:
Here, gcd(4,6) = 2, because 2 is the largest positive integer that divides both 4 and 6.
The first line of input contains two integers ‘n’ and 'm'.
Return an integer as described in the problem statement.
You don’t need to print anything, it has already been taken care of, just complete the given function.
9 6
3
gcd(6,9) is 3, as 3 is the largest positive integer that divides both 6 and 9.
2 5
1
Try to solve this in O(log(n))
0 <= ‘n’ <= 10^5
Time Limit: 1 sec
Iterate through all the integers from 1 to min(n,m).
Approach:
We can simply iterate through all the integers from 1 to min(n,m) and check for the greatest integer that divides both ‘n’ and ‘m’.
The steps are as follows:
function calcGCD(int n, int m)
O(min(n,m)), where ‘n’ and ‘m’ are the given numbers.
We are iterating via ‘i’ from 1 to min(n,m).
Hence, the time complexity is O(min(n,m)).
O(1).
We are not using any extra space.
Hence, the space complexity is O(1).
Interview problems
gcd or hcf
public class Solution {
public static int calcGCD(int n, int m){
while(m>0 && n>0){
if(m>n){
m=m%n;
}
else{
n=n%m;
}
}
if(m==0){
return n;
}
else return m;
}
}
Interview problems
GCD or HCF
int calcGCD(int n, int m){
for(int i = min(n,m); i>0; i--){
if(n%i==0 && m%i==0){
return i;
}
}
return 1;
}
Interview problems
C++ Runtime graph( beats 100% and runtime 32ms) and memory graph(beats 91.19% and memory 11681 kB)
int calcGCD(int n, int m){
// Write your code here.
while (n>0 && m>0){
if (n>m){
n=n%m;
}
else{
m=m%n;
}
}
if (n==0) return m;
return n;
}
Interview problems
beats 100% c++ code
int calcGCD(int n, int m){
while (n > 0 && m > 0) {
if (n > m)
n = n % m;
else
m = m % n;
}
if (n == 0) return m;
return n;
}
Interview problems
using java. Euclidean algorithmused
public class Solution {
public static int calcGCD(int n, int m){
// Write your code here.
int a=0;
int b=0;
if(n>m){
a=n;
b=m;
}
else{
a=m;
b=n;
}
while(true){
int digit=a%b;
if(digit==0){
return b;
}
a=b;
b=digit;
}
}
}
Interview problems
Code of Time Complexity O(log(n)).
public class Solution {
public static int calcGCD(int n, int m){
// Write your code here.
if(m>n){
int temp = m;
m=n;
n=temp;
}
int r;
for(;;){
if(n%m==0){
break;
}
else{
r=n%m;
n=m;
m=r;
}
}
return m;
}
}
Interview problems
java Solution
public class Solution {
public static int calcGCD(int n, int m){
int gcd=2, count=1;
if(n==m)
{
return n;
}
else{
while(gcd<n || gcd<m)
{
if(n%gcd==0 && m%gcd==0)
{
count=gcd;
}
gcd=gcd+1;
}
return count;
}
}
}
Interview problems
why is the timecomplexity for the following code high
int calcGCD(int n, int m){
while(n!=m)
{
if(n>m)
n=n-m;
else
m=m-n;
}
return m;
}
Interview problems
Euclidean Algorithm Java Optimal code
public class Solution {
public static int calcGCD(int n, int m){
// Write your code here.
while(n>0 && m > 0){
if(n>m) n = n%m;
else m = m% n;
}
if(n==0) return m;
else return n ;
}
}
Interview problems
hcf using recursion
int calcGCD(int n, int m){
// Write your code here.
int rem;
rem=n%m;
if(rem==0)
return m;
else
return calcGCD(m,rem );
}