Given a number N, the task is to find two numbers a and b such that a + b = N and LCM(a, b) is minimum.
Input: N = 15
Output: a = 5, b = 10
The pair 5, 10 has a sum of 15 and their LCM is 10 which is the minimum possible.
Input: N = 4
Output: a = 2, b = 2
The pair 2, 2 has a sum of 4 and their LCM is 2 which is the minimum possible.
- If N is a Prime Number then the answer is 1 and N – 1 because in any other cases either a + b > N or LCM( a, b) is > N – 1. This is because if N is prime then it implies that N is odd. So a and b, any one of them must be odd and other even. Therefore, LCM(a, b) must be greater than N ( if not 1 and N – 1) as 2 will always be a factor.
- If N is not a prime number then choose a, b such that their GCD is maximum, because of the formula LCM(a, b) = a*b / GCD (a, b). So, in order to minimize LCM(a, b) we must maximize GCD(a, b).
- If x is a divisor of N, then by simple mathematics a and b can be represented as N / x and N / x*( x – 1) respectively. Now as a = N / x and b = N / x * (x – 1), so their GCD comes out as N / x. To maximize this GCD, take the smallest possible x or smallest possible divisor of N.
Below is the implementation of the above approach:
Time Complexity: O(sqrt(N))
Auxiliary Space: O(1)
- Sum of LCM(1, n), LCM(2, n), LCM(3, n), ... , LCM(n, n)
- Minimum replacement of pairs by their LCM required to reduce given array to its LCM
- Find three integers less than or equal to N such that their LCM is maximum
- Possible values of Q such that, for any value of R, their product is equal to X times their sum
- Number of pairs such that their HCF and LCM is equal
- Count of pairs upto N such whose LCM is not equal to their product for Q queries
- Count of pairs in a given range with sum of their product and sum equal to their concatenated number
- Find two distinct numbers such that their LCM lies in given range
- Count pairs from 1 to N such that their Sum is divisible by their XOR
- Minimum number of primes required such that their sum is equal to N
- Find the first N integers such that the sum of their digits is equal to 10
- Find any K distinct odd integers such that their sum is equal to N
- Find K numbers with sum equal to N and sum of their squares maximized
- Print any pair of integers with sum of GCD and LCM equals to N
- Find two numbers such that difference of their squares equal to N
- Find any pair with given GCD and LCM
- Split N powers of 2 into two subsets such that their difference of sum is minimum
- Split array into two subarrays such that difference of their sum is minimum
- Minimum possible value of max(A, B) such that LCM(A, B) = C
- Maximize array sum by replacing equal adjacent pairs by their sum and X respectively
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.