Find pair with maximum GCD for integers in range 2 to N
Last Updated :
14 Apr, 2023
Given a number N, the task is to find a pair of integers in the range [2, N] with maximum GCD.
Examples:
Input: N = 10
Output: 5
Explanation:
Maximum possible GCD between all possible pairs is 5 which occurs for the pair (10, 5).
Input: N = 13
Output: 6
Explanation:
Maximum possible GCD between all possible pairs is 6 which occurs for the pair (12, 6).
Approach:
Follow the steps below to solve the problem:
- If N is even, return the pair {N, N / 2}.
Illustration:
If N = 10, Maximum possible GCD for any pair is 5( for the pair {5, 10}).
If N = 20, Maximum possible GCD for any pair is 10( for the pair {20, 10}).
- If N is odd, then return the pair{N – 1, (N – 1) / 2}.
Illustration:
If N = 11, Maximum possible GCD for any pair is 5( for the pair {5, 10}).
If N = 21, Maximum possible GCD for any pair is 10( for the pair {20, 10}).
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void solve( int N)
{
if (N % 2 == 0) {
cout << N / 2 << " "
<< N << endl;
}
else {
cout << (N - 1) / 2 << " "
<< (N - 1) << endl;
}
}
int main()
{
int N = 10;
solve(N);
return 0;
}
|
Java
class GFG{
static void solve( int N)
{
if (N % 2 == 0 )
{
System.out.print(N / 2 + " " +
N + "\n" );
}
else
{
System.out.print((N - 1 ) / 2 + " " +
(N - 1 ) + "\n" );
}
}
public static void main(String[] args)
{
int N = 10 ;
solve(N);
}
}
|
Python3
def solve(N):
if (N % 2 = = 0 ):
print (N / / 2 , N)
else :
print ((N - 1 ) / / 2 , (N - 1 ))
N = 10
solve(N)
|
C#
using System;
class GFG{
static void solve( int N)
{
if (N % 2 == 0)
{
Console.Write(N / 2 + " " +
N + "\n" );
}
else
{
Console.Write((N - 1) / 2 + " " +
(N - 1) + "\n" );
}
}
public static void Main(String[] args)
{
int N = 10;
solve(N);
}
}
|
Javascript
<script>
function solve(N) {
if (N % 2 == 0) {
document.write(N / 2 + " " + N + "<br/>" );
}
else {
document.write((N - 1) / 2 + " " + (N - 1) + "<br/>" );
}
}
var N = 10;
solve(N);
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
New Approach:-
Another approach to solve this problem is to use a nested loop to iterate over all possible pairs of integers in the range [2, N] and calculate their GCD using the Euclidean algorithm. We can keep track of the maximum GCD seen so far and the pair of integers that produced it. The time complexity of this approach would be O(N^2) since we are iterating over N^2 pairs of integers.
Algorithm:
- Define a function “gcd” to calculate the GCD of two numbers.
- Define a function “findMaxGCDPair” that takes an integer N as input.
- Initialize the maximum GCD to -1 and x, y to 0.
- Loop through all pairs of integers i and j in the range [2, N].
- Calculate the GCD of i and j using the “gcd” function.
- If the GCD is greater than the current maximum GCD, update the maximum GCD and set x and y to i and j, respectively.
- Print the pair (x, y) with the maximum GCD.
- In the main function, call the “findMaxGCDPair” function with the input value N.
- End of the program.
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);
}
void findMaxGCDPair( int N) {
int max_gcd = -1;
int x, y;
for ( int i = 2; i <= N; i++) {
for ( int j = i + 1; j <= N; j++) {
int cur_gcd = gcd(i, j);
if (cur_gcd > max_gcd) {
max_gcd = cur_gcd;
x = i;
y = j;
}
}
}
cout << "Pair with maximum GCD: " << x << " " << y << endl;
}
int main() {
int N = 10;
findMaxGCDPair(N);
return 0;
}
|
Java
import java.util.*;
public class Main {
public static int gcd( int a, int b) {
if (b == 0 ) {
return a;
}
return gcd(b, a % b);
}
public static void findMaxGCDPair( int N) {
int max_gcd = - 1 ;
int x = 0 , y = 0 ;
for ( int i = 2 ; i <= N; i++) {
for ( int j = i + 1 ; j <= N; j++) {
int cur_gcd = gcd(i, j);
if (cur_gcd > max_gcd) {
max_gcd = cur_gcd;
x = i;
y = j;
}
}
}
System.out.println( "Pair with maximum GCD: " + x + " " + y);
}
public static void main(String[] args) {
int N = 10 ;
findMaxGCDPair(N);
}
}
|
C#
using System;
public class Program
{
public static int gcd( int a, int b)
{
if (b == 0)
{
return a;
}
return gcd(b, a % b);
}
public static void findMaxGCDPair( int N)
{
int max_gcd = -1;
int x = 0, y = 0;
for ( int i = 2; i <= N; i++)
{
for ( int j = i + 1; j <= N; j++)
{
int cur_gcd = gcd(i, j);
if (cur_gcd > max_gcd)
{
max_gcd = cur_gcd;
x = i;
y = j;
}
}
}
Console.WriteLine( "Pair with maximum GCD: " + x + " " + y);
}
public static void Main()
{
int N = 10;
findMaxGCDPair(N);
}
}
|
Javascript
function gcd(a, b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
function findMaxGCDPair(N) {
let max_gcd = -1;
let x, y;
for (let i = 2; i <= N; i++) {
for (let j = i + 1; j <= N; j++) {
let cur_gcd = gcd(i, j);
if (cur_gcd > max_gcd) {
max_gcd = cur_gcd;
x = i;
y = j;
}
}
}
console.log( "Pair with maximum GCD: " + x + " " + y);
}
let N = 10;
findMaxGCDPair(N);
|
Python
def gcd(a, b):
if b = = 0 :
return a
return gcd(b, a % b)
def findMaxGCDPair(N):
max_gcd = - 1
x, y = 0 , 0
for i in range ( 2 , N + 1 ):
for j in range (i + 1 , N + 1 ):
cur_gcd = gcd(i, j)
if cur_gcd > max_gcd:
max_gcd = cur_gcd
x, y = i, j
print ( "Pair with maximum GCD: {} {}" . format (x, y))
N = 10
findMaxGCDPair(N)
|
Output
Pair with maximum GCD: 5 10
“Note that this approach is not very efficient for large values of N, since the time complexity is quadratic. However, it can be useful for small values of N.”
Time Complexity: O(N2)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...