Find the pair (a, b) with minimum LCM such that their sum is equal to N
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.
Examples:
Input: N = 15
Output: a = 5, b = 10
Explanation:
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
Explanation:
The pair 2, 2 has a sum of 4 and their LCM is 2 which is the minimum possible.
Approach: The idea is to use the concept of GCD and LCM. Below are the steps:
- 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:
C++
#include <bits/stdc++.h>
using namespace std;
bool prime( int n)
{
if (n == 1)
return false ;
for ( int i = 2; i * i <= n; i++) {
if (n % i == 0)
return false ;
}
return true ;
}
void minDivisor( int n)
{
if (prime(n)) {
cout << 1 << " " << n - 1;
}
else {
for ( int i = 2; i * i <= n; i++) {
if (n % i == 0) {
cout << n / i << " "
<< n / i * (i - 1);
break ;
}
}
}
}
int main()
{
int N = 4;
minDivisor(N);
return 0;
}
|
Java
import java.io.*;
public class GFG{
static boolean prime( int n)
{
if (n == 1 )
return false ;
for ( int i = 2 ; i * i <= n; i++)
{
if (n % i == 0 )
return false ;
}
return true ;
}
static void minDivisor( int n)
{
if (prime(n))
{
System.out.print( 1 + " " + (n - 1 ));
}
else
{
for ( int i = 2 ; i * i <= n; i++)
{
if (n % i == 0 )
{
System.out.print(n / i + " " +
(n / i * (i - 1 )));
break ;
}
}
}
}
public static void main(String[] args)
{
int N = 4 ;
minDivisor(N);
}
}
|
Python3
def prime(n):
if (n = = 1 ):
return False
for i in range ( 2 , n + 1 ):
if i * i > n:
break
if (n % i = = 0 ):
return False
return True
def minDivisor(n):
if (prime(n)):
print ( 1 , n - 1 )
else :
for i in range ( 2 , n + 1 ):
if i * i > n:
break
if (n % i = = 0 ):
print (n / / i, n / / i * (i - 1 ))
break
N = 4
minDivisor(N)
|
C#
using System;
class GFG{
static bool prime( int n)
{
if (n == 1)
return false ;
for ( int i = 2; i * i <= n; i++)
{
if (n % i == 0)
return false ;
}
return true ;
}
static void minDivisor( int n)
{
if (prime(n))
{
Console.Write(1 + " " + (n - 1));
}
else
{
for ( int i = 2; i * i <= n; i++)
{
if (n % i == 0)
{
Console.Write(n / i + " " +
(n / i * (i - 1)));
break ;
}
}
}
}
public static void Main(String[] args)
{
int N = 4;
minDivisor(N);
}
}
|
Javascript
<script>
function prime(n)
{
if (n == 1)
return false ;
for (i = 2; i * i <= n; i++)
{
if (n % i == 0)
return false ;
}
return true ;
}
function minDivisor(n)
{
if (prime(n))
{
document.write(1 + " " + (n - 1));
}
else
{
for (i = 2; i * i <= n; i++)
{
if (n % i == 0)
{
document.write(n / i + " " + (n / i * (i - 1)));
break ;
}
}
}
}
var N = 4;
minDivisor(N);
</script>
|
Time Complexity: O(sqrt(N))
Auxiliary Space: O(1)
Last Updated :
12 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...