Skip to content
Related Articles

Related Articles

Maximum possible GCD for a pair of integers with product N
  • Last Updated : 13 Apr, 2021

Given an integer N, the task is to find the maximum possible GCD among all pair of integers with product N.
Examples:

Input: N=12 
Output:
Explanation: 
All possible pairs with product 12 are {1, 12}, {2, 6}, {3, 4} 
GCD(1, 12) = 1 
GCD(2, 6) = 2 
GCD(3, 4) = 1 
Therefore, the maximum possible GCD = maximum(1, 2, 1) = 2
Input:
Output:
Explanation: 
All possible pairs with product 4 are {1, 4}, {2, 2} 
GCD(1, 4) = 1 
GCD(2, 2) = 2 
Hence, maximum possible GCD = maximum(1, 2) = 2

Naive Approach: 
The simplest approach to solve this problem is to generate all possible pairs with product N and calculate GCD of all such pairs. Finally, print the maximum GCD obtained. 
Time Complexity: O(NlogN) 
Auxiliary Space: O(1)
Efficient Approach: 
The above approach can be optimized by finding all divisors of the given number N. For each pair of divisors obtained, calculate their GCD. Finally, print the maximum GCD obtained.
Follow the steps below to solve the problem:

  • Declare a variable maxGcd to keep track of maximum GCD.
  • Iterate up to √N and for every integer, check if it is a factor of N.
  • If N is divisible by i, calculate GCD of the pair of factors (i, N / i).
  • Comapare with GCD(i, N / i) and update maxGcd.
  • Finally, print maxGcd.

Below is the implementation of the above approach:

C++




// C++ Program to implement
// the above approach
#include <bits/stdc++.h>
using namespace std;
 
// Function to return
/// the maximum GCD
int getMaxGcd(int N)
{
    int maxGcd = INT_MIN, A, B;
 
    // To find all divisors of N
    for (int i = 1; i <= sqrt(N); i++) {
 
        // If i is a factor
        if (N % i == 0) {
            // Store the pair of factors
            A = i, B = N / i;
 
            // Store the maximum GCD
            maxGcd = max(maxGcd, __gcd(A, B));
        }
    }
 
    // Return the maximum GCD
    return maxGcd;
}
 
// Driver Code
int main()
{
 
    int N = 18;
    cout << getMaxGcd(N);
 
    return 0;
}

Java




// Java program to implement
// the above approach
import java.util.*;
 
class GFG{
     
static int gcd(int a, int b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);
}
     
// Function to return
// the maximum GCD
static int getMaxGcd(int N)
{
    int maxGcd = Integer.MIN_VALUE, A, B;
     
    // To find all divisors of N
    for(int i = 1; i <= Math.sqrt(N); i++)
    {
         
        // If i is a factor
        if (N % i == 0)
        {
             
            // Store the pair of factors
            A = i;
            B = N / i;
         
            // Store the maximum GCD
            maxGcd = Math.max(maxGcd, gcd(A, B));
        }
    }
     
    // Return the maximum GCD
    return maxGcd;
}
     
// Driver Code
public static void main(String s[])
{
    int N = 18;
     
    System.out.println(getMaxGcd(N));
}
}
 
// This code is contributed by rutvik_56

Python3




# Python3 program to implement
# the above approach
import sys
import math
 
# Function to return
# the maximum GCD
def getMaxGcd(N):
     
    maxGcd = -sys.maxsize - 1
 
    # To find all divisors of N
    for i in range(1, int(math.sqrt(N)) + 1):
 
        # If i is a factor
        if (N % i == 0):
             
            # Store the pair of factors
            A = i
            B = N // i
 
            # Store the maximum GCD
            maxGcd = max(maxGcd, math.gcd(A, B))
         
    # Return the maximum GCD
    return maxGcd
 
# Driver Code
N = 18
 
print(getMaxGcd(N))
 
# This code is contributed by code_hunt

C#




// C# program to implement
// the above approach
using System;
class GFG{
     
static int gcd(int a, int b)
{
    if (b == 0)
        return a;
    return gcd(b, a % b);
}
     
// Function to return
// the maximum GCD
static int getMaxGcd(int N)
{
    int maxGcd = int.MinValue, A, B;
     
    // To find all divisors of N
    for(int i = 1; i <= Math.Sqrt(N); i++)
    {
         
        // If i is a factor
        if (N % i == 0)
        {
             
            // Store the pair of factors
            A = i;
            B = N / i;
         
            // Store the maximum GCD
            maxGcd = Math.Max(maxGcd, gcd(A, B));
        }
    }
     
    // Return the maximum GCD
    return maxGcd;
}
     
// Driver Code
public static void Main(String []s)
{
    int N = 18;
     
    Console.WriteLine(getMaxGcd(N));
}
}
 
// This code is contributed by sapnasingh4991

Javascript




<script>
// javascript program to implement
// the above approach
 
    function gcd(a , b)
    {
        if (b == 0)
            return a;
        return gcd(b, a % b);
    }
 
    // Function to return
    // the maximum GCD
    function getMaxGcd(N)
    {
        var maxGcd = Number.MIN_VALUE, A, B;
 
        // To find all divisors of N
        for (i = 1; i <= Math.sqrt(N); i++)
        {
 
            // If i is a factor
            if (N % i == 0)
            {
 
                // Store the pair of factors
                A = i;
                B = N / i;
 
                // Store the maximum GCD
                maxGcd = Math.max(maxGcd, gcd(A, B));
            }
        }
 
        // Return the maximum GCD
        return maxGcd;
    }
 
    // Driver Code
        var N = 18;
        document.write(getMaxGcd(N));
 
// This code is contributed by aashish1995
</script>
Output: 
3

Time Complexity: O(√N*log(N)) 
Auxiliary Space: O(1)
 

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :