# Maximum possible GCD for a pair of integers with sum N

• Last Updated : 16 Dec, 2022

Given an integer N, the task is to find the maximum possible GCD of a pair of integers such that their sum is N.

Examples :

Input: N = 30
Output: 15
Explanation: GCD of (15, 15) is 15, which is the maximum possible GCD

Input: N = 33
Output: 11
Explanation: GCD of (11, 22) is 11, which is the maximum possible GCD

Naive Approach:
The simplest approach to solve this problem is to calculate GCD for all pair of integers with sum N and find the maximum possible GCD among them.
Time complexity: O(N2logN)
Auxiliary Space: O(1)

Efficient Approach:
Follow the steps given below to optimize the above approach:

• Iterate up to √N and find the largest proper factor of N.
• If N is prime, i.e. no factor could be obtained, print 1, as all pairs are co-prime.
• Otherwise, print the largest possible factor as the answer.

Below is the implementation of the above approach:

## C++

 // C++ Program to find the maximum// possible GCD of any pair with// sum N#include using namespace std; // Function to find the required// GCD valueint maxGCD(int N){    for (int i = 2; i * i <= N; i++) {         // If i is a factor of N        if (N % i == 0) {             // Return the largest            // factor possible            return N / i;        }    }     // If N is a prime number    return 1;} // Driver Codeint main(){    int N = 33;    cout << "Maximum Possible GCD value is : "        << maxGCD(N) << endl;    return 0;}

## Java

 // Java program to find the maximum// possible GCD of any pair with// sum Nimport java.io.*;public class GFG{ // Function to find the required// GCD valuestatic int maxGCD(int N){    for(int i = 2; i * i <= N; i++)    {         // If i is a factor of N        if (N % i == 0)        {             // Return the largest            // factor possible            return N / i;        }    }     // If N is a prime number    return 1;}     // Driver Codepublic static void main(String[] args){    int N = 33;    System.out.println("Maximum Possible GCD " +                       "value is : " + maxGCD(N));}} // This code is conhtributed by rutvik_56

## Python3

 # Python3 program to find the maximum# possible GCD of any pair with# sum N # Function to find the required# GCD valuedef maxGCD(N):         i = 2    while(i * i <= N):         # If i is a factor of N        if (N % i == 0):             # Return the largest            # factor possible            return N // i                 i += 1     # If N is a prime number    return 1 # Driver CodeN = 33 print("Maximum Possible GCD value is : ",       maxGCD(N)) # This code is contributed by code_hunt

## C#

 // C# program to find the maximum// possible GCD of any pair with// sum Nusing System; class GFG{ // Function to find the required// GCD valuestatic int maxGCD(int N){    for(int i = 2; i * i <= N; i++)    {         // If i is a factor of N        if (N % i == 0)        {             // Return the largest            // factor possible            return N / i;        }    }     // If N is a prime number    return 1;}     // Driver Codepublic static void Main(String[] args){    int N = 33;    Console.WriteLine("Maximum Possible GCD " +                      "value is : " + maxGCD(N));}} // This code is contributed by Princi Singh

## Javascript



Output:

Maximum Possible GCD value is : 11

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

My Personal Notes arrow_drop_up