Given an integer N, the task is to find the maximum possible GCD of a pair of integers such that their sum is N.
Input: N = 30
Explanation: GCD of (15, 15) is 15, which is the maximum possible GCD
Input: N = 33
Explanation: GCD of (11, 22) is 11, which is the maximum possible GCD
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)
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:
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.
- Pair of integers having least GCD among all given pairs having GCD exceeding K
- Maximum possible GCD for a pair of integers with product N
- Find pair with maximum GCD for integers in range 2 to N
- Print any pair of integers with sum of GCD and LCM equals to N
- Minimum LCM and GCD possible among all possible sub-arrays
- Maximum GCD of N integers with given product
- Find pair with maximum GCD in an array
- Pair with maximum GCD from two arrays
- Smallest subsequence having GCD equal to GCD of given array
- Subsequence of size k with maximum possible GCD
- Maximum possible GCD after replacing at most one element in the given array
- Find a pair with maximum product in array of Integers
- Count of all possible pairs having sum of LCM and GCD equal to N
- Sum of GCD of all possible sequences
- Recursive program to print formula for GCD of n integers
- Find any pair with given GCD and LCM
- Print N lines of 4 numbers such that every pair among 4 numbers has a GCD K
- Check if there is any pair in a given range with GCD is divisible by k
- Find original numbers from gcd() every pair
- Print all possible ways to write N as sum of two or more positive integers
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.