Given two positive integer a and b and a range [low, high]. The task is to find the greatest common divisor of a and b which lie in the given range. If no divisor exist in the range, print -1.
Examples:
Input : a = 9, b = 27, low = 1, high = 5 Output : 3 3 is the highest number that lies in range [1, 5] and is common divisor of 9 and 27. Input : a = 9, b = 27, low = 10, high = 11 Output : -1
The idea is to find the Greatest Common Divisor GCD(a, b) of a and b. Now observe, divisor of GCD(a, b) is also the divisor of a and b. So, we will iterate a loop i from 1 to sqrt(GCD(a, b)) and check if i divides GCD(a, b). Also, observe if i is divisor of GCD(a, b) then GCD(a, b)/i will also be divisor. So, for each iteration, if i divides GCD(a, b), we will find maximum of i and GCD(a, b)/i if they lie in the range.
Below is the implementation of this approach:
// CPP Program to find the Greatest Common divisor // of two number which is in given range #include <bits/stdc++.h> using namespace std;
// Return the greatest common divisor // of two numbers int gcd( int a, int b)
{ if (b == 0)
return a;
return gcd(b, a % b);
} // Return the greatest common divisor of a // and b which lie in the given range. int maxDivisorRange( int a, int b, int l, int h)
{ int g = gcd(a, b);
int res = -1;
// Loop from 1 to sqrt(GCD(a, b).
for ( int i = l; i * i <= g && i <= h; i++)
// if i divides the GCD(a, b), then
// find maximum of three numbers res,
// i and g/i
if (g % i == 0)
res = max({res, i, g / i});
return res;
} // Driven Program int main()
{ int a = 3, b = 27, l = 1, h = 5;
cout << maxDivisorRange(a, b, l, h) << endl;
return 0;
} |
// Java Program to find the Greatest Common // divisor of two number which is in given // range import java.io.*;
class GFG {
// Return the greatest common divisor
// of two numbers
static int gcd( int a, int b)
{
if (b == 0 )
return a;
return gcd(b, a % b);
}
// Return the greatest common divisor of a
// and b which lie in the given range.
static int maxDivisorRange( int a, int b,
int l, int h)
{
int g = gcd(a, b);
int res = - 1 ;
// Loop from 1 to sqrt(GCD(a, b).
for ( int i = l; i * i <= g && i <= h; i++)
// if i divides the GCD(a, b), then
// find maximum of three numbers res,
// i and g/i
if (g % i == 0 )
res = Math.max(res,
Math.max(i, g / i));
return res;
}
// Driven Program
public static void main (String[] args)
{
int a = 3 , b = 27 , l = 1 , h = 5 ;
System.out.println(
maxDivisorRange(a, b, l, h));
}
} // This code is contributed by anuj_67. |
# Python3 Program to find the # Greatest Common divisor # of two number which is # in given range # Return the greatest common # divisor of two numbers def gcd(a, b):
if (b = = 0 ):
return a;
return gcd(b, a % b);
# Return the greatest common # divisor of a and b which # lie in the given range. def maxDivisorRange(a, b, l, h):
g = gcd(a, b);
res = - 1 ;
# Loop from 1 to
# sqrt(GCD(a, b).
i = l;
while (i * i < = g and i < = h):
# if i divides the GCD(a, b),
# then find maximum of three
# numbers res, i and g/i
if (g % i = = 0 ):
res = max (res, max (i, g / i));
i + = 1 ;
return int (res);
# Driver Code if __name__ = = "__main__" :
a = 3 ;
b = 27 ;
l = 1 ;
h = 5 ;
print (maxDivisorRange(a, b, l, h));
# This code is contributed by mits |
// C# Program to find the Greatest Common // divisor of two number which is in given // range using System;
class GFG {
// Return the greatest common divisor
// of two numbers
static int gcd( int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
// Return the greatest common divisor of a
// and b which lie in the given range.
static int maxDivisorRange( int a, int b,
int l, int h)
{
int g = gcd(a, b);
int res = -1;
// Loop from 1 to sqrt(GCD(a, b).
for ( int i = l; i * i <= g && i <= h; i++)
// if i divides the GCD(a, b), then
// find maximum of three numbers res,
// i and g/i
if (g % i == 0)
res = Math.Max(res,
Math.Max(i, g / i));
return res;
}
// Driven Program
public static void Main ()
{
int a = 3, b = 27, l = 1, h = 5;
Console.WriteLine(
maxDivisorRange(a, b, l, h));
}
} // This code is contributed by anuj_67. |
<?php // PHP Program to find the // Greatest Common divisor // of two number which is // in given range // Return the greatest common // divisor of two numbers function gcd( $a , $b )
{ if ( $b == 0)
return $a ;
return gcd( $b , $a % $b );
} // Return the greatest common // divisor of a and b which // lie in the given range. function maxDivisorRange( $a , $b ,
$l , $h )
{ $g = gcd( $a , $b );
$res = -1;
// Loop from 1 to
// sqrt(GCD(a, b).
for ( $i = $l ; $i * $i <= $g and
$i <= $h ; $i ++)
// if i divides the GCD(a, b),
// then find maximum of three
// numbers res, i and g/i
if ( $g % $i == 0)
$res = max( $res ,
max( $i , $g / $i ));
return $res ;
} // Driver Code $a = 3; $b = 27;
$l = 1; $h = 5;
echo maxDivisorRange( $a , $b , $l , $h );
// This code is contributed by anuj_67. ?> |
<script> // JavaScript Program to find the Greatest Common // divisor of two number which is in given // range // Return the greatest common divisor
// of two numbers
function gcd(a , b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
// Return the greatest common divisor of a
// and b which lie in the given range.
function maxDivisorRange(a , b , l , h) {
var g = gcd(a, b);
var res = -1;
// Loop from 1 to sqrt(GCD(a, b).
for (i = l; i * i <= g && i <= h; i++)
// if i divides the GCD(a, b), then
// find maximum of three numbers res,
// i and g/i
if (g % i == 0)
res = Math.max(res, Math.max(i, g / i));
return res;
}
// Driven Program
var a = 3, b = 27, l = 1, h = 5;
document.write(maxDivisorRange(a, b, l, h));
// This code is contributed by todaysgaurav </script> |
3
Time complexity: O(log(min(a,b)) + min(?m,h)). where m is the gcd of inputs a and b.
Auxiliary Space: O(1)