Maximum possible GCD for a pair of integers with sum N

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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ Program to find the maximum 
// possible GCD of any pair with 
// sum N 
#include <bits/stdc++.h> 
using namespace std; 
  
// Function to find the required 
// GCD value 
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 Code 
int main() 
    int N = 33; 
    cout << "Maximum Possible GCD value is : "
        << maxGCD(N) << endl; 
    return 0; 

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to find the maximum
// possible GCD of any pair with
// sum N
class GFG{
  
// Function to find the required
// GCD value
static 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 Code
public 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to find the maximum 
# possible GCD of any pair with 
# sum N 
  
# Function to find the required 
# GCD value 
def 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 += 1
  
    # If N is a prime number 
    return 1
  
# Driver Code 
N = 33
  
print("Maximum Possible GCD value is : ",
       maxGCD(N))
  
# This code is contributed by code_hunt

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to find the maximum
// possible GCD of any pair with
// sum N
using System;
  
class GFG{
  
// Function to find the required
// GCD value
static 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 Code
public static void Main(String[] args)
{
    int N = 33;
    Console.WriteLine("Maximum Possible GCD "
                      "value is : " + maxGCD(N));
}
}
  
// This code is contributed by Princi Singh

chevron_right


Output: 

Maximum Possible GCD value is : 11

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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.