Find the maximum GCD possible for some pair in a given range [L, R]
Last Updated :
09 Apr, 2022
Given a range L to R, the task is to find the maximum possible value of GCD(X, Y) such that X and Y belongs to the given range, i.e. L ? X < Y ? R.
Examples:
Input: L = 101, R = 139
Output:
34
Explanation:
For X = 102 and Y = 136, the GCD of x and y is 34, which is the maximum possible.
Input: L = 8, R = 14
Output:
7
Naive Approach: Every pair that can be formed from L to R, can be iterated over using two nested loops and the maximum GCD can be found.
Time Complexity: O((R-L)2Log(R))
Auxiliary Space: O(1)
Efficient Approach: Follow the below steps to solve the problem:
- Let the maximum GCD be Z, therefore, X and Y are both multiples of Z. Conversely if there are two or more multiples of Z in the segment [L, R], then (X, Y) can be chosen such that GCD(x, y) is maximum by choosing consecutive multiples of Z in [L, R].
- Iterate from R to 1 and find whether any of them has at least two multiples in the range [L, R]
- The multiples of Z between L and R can be calculated using the following formula:
- Number of Multiples of Z in [L, R] = Number of multiples of Z in [1, R] – Number of Multiples of Z in [1, L-1]
- This can be written as :
- No. of Multiples of Z in [L, R] = floor(R/Z) – floor((L-1)/Z)
- We can further optimize this by limiting the iteration from R/2 to 1 as the greatest possible GCD is R/2 (with multiples R/2 and R)
Below is the implementation of the above 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 maxGCDInRange( int L, int R)
{
int ans = 1;
for ( int Z = R/2; Z >= 1; Z--) {
if ((R / Z) - ((L - 1) / Z) > 1) {
ans = Z;
break ;
}
}
return ans;
}
int main()
{
int L = 102;
int R = 139;
cout << maxGCDInRange(L, R);
return 0;
}
|
Java
import java.io.*;
class GFG {
public static int GCD( int a, int b)
{
if (b == 0 )
return a;
return GCD(b, a % b);
}
public static int maxGCDInRange( int L, int R)
{
int ans = 1 ;
for ( int Z = R/ 2 ; Z >= 1 ; Z--) {
if ((R / Z) - ((L - 1 ) / Z) > 1 ) {
ans = Z;
break ;
}
}
return ans;
}
public static void main(String[] args)
{
int L = 102 ;
int R = 139 ;
System.out.println(maxGCDInRange(L, R));
}
|
Python3
def GCD(a, b):
if (b = = 0 ):
return a
return GCD(b, a % b)
def maxGCDInRange(L, R):
ans = 1
for Z in range (R / / 2 , 1 , - 1 ):
if (((R / / Z) - ((L - 1 ) / / Z )) > 1 ):
ans = Z
break
return ans
L = 102
R = 139
print (maxGCDInRange(L, R))
|
C#
using System;
class GFG{
public static int GCD( int a, int b)
{
if (b == 0)
return a;
return GCD(b, a % b);
}
public static int maxGCDInRange( int L, int R)
{
int ans = 1;
for ( int Z = R/2; Z >= 1; Z--)
{
if ((R / Z) - ((L - 1) / Z) > 1)
{
ans = Z;
break ;
}
}
return ans;
}
public static void Main()
{
int L = 102;
int R = 139;
Console.Write(maxGCDInRange(L, R));
}
}
|
Javascript
<script>
function GCD( a, b)
{
if (b == 0)
return a;
return GCD(b, a % b);
}
function maxGCDInRange(L, R)
{
let ans = 1;
for (let Z = parseInt((R / 2)); Z >= 1; Z--)
{
if (parseInt((R / Z)) - parseInt((L - 1) / Z ) > 1)
{
ans = Z;
break ;
}
}
return ans;
}
let L = 102;
let R = 139;
document.write(maxGCDInRange(L, R));
</script>
|
Time Complexity: O(R)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...