Find the GCD that lies in given range
Last Updated :
14 Feb, 2023
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:
C++
#include <bits/stdc++.h>
using namespace std;
int gcd( int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
int maxDivisorRange( int a, int b, int l, int h)
{
int g = gcd(a, b);
int res = -1;
for ( int i = l; i * i <= g && i <= h; i++)
if (g % i == 0)
res = max({res, i, g / i});
return res;
}
int main()
{
int a = 3, b = 27, l = 1, h = 5;
cout << maxDivisorRange(a, b, l, h) << endl;
return 0;
}
|
Java
import java.io.*;
class GFG {
static int gcd( int a, int b)
{
if (b == 0 )
return a;
return gcd(b, a % b);
}
static int maxDivisorRange( int a, int b,
int l, int h)
{
int g = gcd(a, b);
int res = - 1 ;
for ( int i = l; i * i <= g && i <= h; i++)
if (g % i == 0 )
res = Math.max(res,
Math.max(i, g / i));
return res;
}
public static void main (String[] args)
{
int a = 3 , b = 27 , l = 1 , h = 5 ;
System.out.println(
maxDivisorRange(a, b, l, h));
}
}
|
Python3
def gcd(a, b):
if (b = = 0 ):
return a;
return gcd(b, a % b);
def maxDivisorRange(a, b, l, h):
g = gcd(a, b);
res = - 1 ;
i = l;
while (i * i < = g and i < = h):
if (g % i = = 0 ):
res = max (res, max (i, g / i));
i + = 1 ;
return int (res);
if __name__ = = "__main__" :
a = 3 ;
b = 27 ;
l = 1 ;
h = 5 ;
print (maxDivisorRange(a, b, l, h));
|
C#
using System;
class GFG {
static int gcd( int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
static int maxDivisorRange( int a, int b,
int l, int h)
{
int g = gcd(a, b);
int res = -1;
for ( int i = l; i * i <= g && i <= h; i++)
if (g % i == 0)
res = Math.Max(res,
Math.Max(i, g / i));
return res;
}
public static void Main ()
{
int a = 3, b = 27, l = 1, h = 5;
Console.WriteLine(
maxDivisorRange(a, b, l, h));
}
}
|
PHP
<?php
function gcd( $a , $b )
{
if ( $b == 0)
return $a ;
return gcd( $b , $a % $b );
}
function maxDivisorRange( $a , $b ,
$l , $h )
{
$g = gcd( $a , $b );
$res = -1;
for ( $i = $l ; $i * $i <= $g and
$i <= $h ; $i ++)
if ( $g % $i == 0)
$res = max( $res ,
max( $i , $g / $i ));
return $res ;
}
$a = 3; $b = 27;
$l = 1; $h = 5;
echo maxDivisorRange( $a , $b , $l , $h );
?>
|
Javascript
<script>
function gcd(a , b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
function maxDivisorRange(a , b , l , h) {
var g = gcd(a, b);
var res = -1;
for (i = l; i * i <= g && i <= h; i++)
if (g % i == 0)
res = Math.max(res, Math.max(i, g / i));
return res;
}
var a = 3, b = 27, l = 1, h = 5;
document.write(maxDivisorRange(a, b, l, h));
</script>
|
Time complexity: O(log(min(a,b)) + min(?m,h)). where m is the gcd of inputs a and b.
Auxiliary Space: O(1)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...