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 GCDInput: 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 <bits/stdc++.h>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 Nclass 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 = 33print("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
<script>// Javascript program to find the maximum// possible GCD of any pair with// sum N// Function to find the required// GCD valuefunction maxGCD(N){ for(var 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 codevar N = 33;document.write("Maximum Possible GCD " + "value is :" + maxGCD(N));// This code is contributed by Ankita saini. </script> |
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 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.


