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 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 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; } |
Java
// Java program to find the maximum // possible GCD of any pair with // sum N import java.io.*; public 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 |
Python3
# 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 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 |
C#
// 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 |
Javascript
<script> // Javascript program to find the maximum // possible GCD of any pair with // sum N // Function to find the required // GCD value function 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 code var N = 33; document.write( "Maximum Possible GCD " + "value is :" + maxGCD(N)); // This code is contributed by Ankita saini. </script> |
Output:
Maximum Possible GCD value is : 11
Time Complexity: O(√N)
Auxiliary Space: O(1)
Please Login to comment...