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 = 15Output:a = 5, b = 10Explanation:The pair 5, 10 has a sum of 15 and their LCM is 10 which is the minimum possible.

Input:N = 4Output:a = 2, b = 2Explanation: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 formulaSo, in order to minimize LCM(a, b) we must**LCM(a, b) = a*b / GCD (a, b)**.maximize GCD(a, b). - If
**x**is a divisor of**N**, then by simple mathematics**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++

`// C++ program for the above approach ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to check if number is ` `// prime or not ` `bool` `prime(` `int` `n) ` `{ ` ` ` `// As 1 is neither prime ` ` ` `// nor composite return false ` ` ` `if` `(n == 1) ` ` ` `return` `false` `; ` ` ` ` ` `// Check if it is divided by any ` ` ` `// number then it is not prime, ` ` ` `// return false ` ` ` `for` `(` `int` `i = 2; i * i <= n; i++) { ` ` ` `if` `(n % i == 0) ` ` ` `return` `false` `; ` ` ` `} ` ` ` ` ` `// Check if n is not divided ` ` ` `// by any number then it is ` ` ` `// prime and hence return true ` ` ` `return` `true` `; ` `} ` ` ` `// Function to find the pair (a, b) ` `// such that sum is N & LCM is minimum ` `void` `minDivisior(` `int` `n) ` `{ ` ` ` ` ` `// Check if the number is prime ` ` ` `if` `(prime(n)) { ` ` ` `cout << 1 << ` `" "` `<< n - 1; ` ` ` `} ` ` ` ` ` `// Now, if it is not prime then ` ` ` `// find the least divisior ` ` ` `else` `{ ` ` ` `for` `(` `int` `i = 2; i * i <= n; i++) { ` ` ` ` ` `// Check if divides n then ` ` ` `// it is a factor ` ` ` `if` `(n % i == 0) { ` ` ` ` ` `// Required output is ` ` ` `// a = n/i & b = n/i*(n-1) ` ` ` `cout << n / i << ` `" "` ` ` `<< n / i * (i - 1); ` ` ` `break` `; ` ` ` `} ` ` ` `} ` ` ` `} ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `N = 4; ` ` ` ` ` `// Function call ` ` ` `minDivisior(N); ` ` ` `return` `0; ` `}` |

*chevron_right*

*filter_none*

## Java

`// Java program for the above approach ` `class` `GFG{ ` ` ` `// Function to check if number is ` `// prime or not ` `static` `boolean` `prime(` `int` `n) ` `{ ` ` ` `// As 1 is neither prime ` ` ` `// nor composite return false ` ` ` `if` `(n == ` `1` `) ` ` ` `return` `false` `; ` ` ` ` ` `// Check if it is divided by any ` ` ` `// number then it is not prime, ` ` ` `// return false ` ` ` `for` `(` `int` `i = ` `2` `; i * i <= n; i++) ` ` ` `{ ` ` ` `if` `(n % i == ` `0` `) ` ` ` `return` `false` `; ` ` ` `} ` ` ` ` ` `// Check if n is not divided ` ` ` `// by any number then it is ` ` ` `// prime and hence return true ` ` ` `return` `true` `; ` `} ` ` ` `// Function to find the pair (a, b) ` `// such that sum is N & LCM is minimum ` `static` `void` `minDivisior(` `int` `n) ` `{ ` ` ` ` ` `// Check if the number is prime ` ` ` `if` `(prime(n)) ` ` ` `{ ` ` ` `System.out.print(` `1` `+ ` `" "` `+ (n - ` `1` `)); ` ` ` `} ` ` ` ` ` `// Now, if it is not prime then ` ` ` `// find the least divisior ` ` ` `else` ` ` `{ ` ` ` `for` `(` `int` `i = ` `2` `; i * i <= n; i++) ` ` ` `{ ` ` ` ` ` `// Check if divides n then ` ` ` `// it is a factor ` ` ` `if` `(n % i == ` `0` `) ` ` ` `{ ` ` ` ` ` `// Required output is ` ` ` `// a = n/i & b = n/i*(n-1) ` ` ` `System.out.print(n / i + ` `" "` `+ ` ` ` `(n / i * (i - ` `1` `))); ` ` ` `break` `; ` ` ` `} ` ` ` `} ` ` ` `} ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `N = ` `4` `; ` ` ` ` ` `// Function call ` ` ` `minDivisior(N); ` `} ` `} ` ` ` `// This code is contributed by Rajput-Ji` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program for the above approach ` ` ` `# Function to check if number is ` `# prime or not ` `def` `prime(n): ` ` ` ` ` `# As 1 is neither prime ` ` ` `# nor composite return false ` ` ` `if` `(n ` `=` `=` `1` `): ` ` ` `return` `False` ` ` ` ` `# Check if it is divided by any ` ` ` `# number then it is not prime, ` ` ` `# return false ` ` ` `for` `i ` `in` `range` `(` `2` `, n ` `+` `1` `): ` ` ` `if` `i ` `*` `i > n: ` ` ` `break` ` ` `if` `(n ` `%` `i ` `=` `=` `0` `): ` ` ` `return` `False` ` ` ` ` `# Check if n is not divided ` ` ` `# by any number then it is ` ` ` `# prime and hence return true ` ` ` `return` `True` ` ` `# Function to find the pair (a, b) ` `# such that sum is N & LCM is minimum ` `def` `minDivisior(n): ` ` ` ` ` `# Check if the number is prime ` ` ` `if` `(prime(n)): ` ` ` `print` `(` `1` `, n ` `-` `1` `) ` ` ` ` ` `# Now, if it is not prime then ` ` ` `# find the least divisior ` ` ` `else` `: ` ` ` `for` `i ` `in` `range` `(` `2` `, n ` `+` `1` `): ` ` ` `if` `i ` `*` `i > n: ` ` ` `break` ` ` ` ` `# Check if divides n then ` ` ` `# it is a factor ` ` ` `if` `(n ` `%` `i ` `=` `=` `0` `): ` ` ` ` ` `# Required output is ` ` ` `# a = n/i & b = n/i*(n-1) ` ` ` `print` `(n ` `/` `/` `i, n ` `/` `/` `i ` `*` `(i ` `-` `1` `)) ` ` ` `break` ` ` `# Driver Code ` `N ` `=` `4` ` ` `# Function call ` `minDivisior(N) ` ` ` `# This code is contributed by mohit kumar 29 ` |

*chevron_right*

*filter_none*

## C#

`// C# program for the above approach ` `using` `System; ` ` ` `class` `GFG{ ` ` ` `// Function to check if number is ` `// prime or not ` `static` `bool` `prime(` `int` `n) ` `{ ` ` ` ` ` `// As 1 is neither prime ` ` ` `// nor composite return false ` ` ` `if` `(n == 1) ` ` ` `return` `false` `; ` ` ` ` ` `// Check if it is divided by any ` ` ` `// number then it is not prime, ` ` ` `// return false ` ` ` `for` `(` `int` `i = 2; i * i <= n; i++) ` ` ` `{ ` ` ` `if` `(n % i == 0) ` ` ` `return` `false` `; ` ` ` `} ` ` ` ` ` `// Check if n is not divided ` ` ` `// by any number then it is ` ` ` `// prime and hence return true ` ` ` `return` `true` `; ` `} ` ` ` `// Function to find the pair (a, b) ` `// such that sum is N & LCM is minimum ` `static` `void` `minDivisior(` `int` `n) ` `{ ` ` ` ` ` `// Check if the number is prime ` ` ` `if` `(prime(n)) ` ` ` `{ ` ` ` `Console.Write(1 + ` `" "` `+ (n - 1)); ` ` ` `} ` ` ` ` ` `// Now, if it is not prime then ` ` ` `// find the least divisior ` ` ` `else` ` ` `{ ` ` ` `for` `(` `int` `i = 2; i * i <= n; i++) ` ` ` `{ ` ` ` ` ` `// Check if divides n then ` ` ` `// it is a factor ` ` ` `if` `(n % i == 0) ` ` ` `{ ` ` ` ` ` `// Required output is ` ` ` `// a = n/i & b = n/i*(n-1) ` ` ` `Console.Write(n / i + ` `" "` `+ ` ` ` `(n / i * (i - 1))); ` ` ` `break` `; ` ` ` `} ` ` ` `} ` ` ` `} ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `int` `N = 4; ` ` ` ` ` `// Function call ` ` ` `minDivisior(N); ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

**Output:**

2 2

**Time Complexity:**** **O(sqrt(N))**Auxiliary Space:** O(1)

## Recommended Posts:

- 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
- 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
- Possible values of Q such that, for any value of R, their product is equal to X times their sum
- Find two distinct numbers such that their LCM lies in given range
- Count of pairs in a given range with sum of their product and sum equal to their concatenated number
- 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
- Print any pair of integers with sum of GCD and LCM equals to N
- Find any pair with given GCD and LCM
- Find two numbers such that difference of their squares equal to N
- Find K numbers with sum equal to N and sum of their squares maximized
- Minimum possible value of max(A, B) such that LCM(A, B) = C
- Generate a pair of integers from a range [L, R] whose LCM also lies within the range
- Count of all possible pairs having sum of LCM and GCD equal to N
- Split N powers of 2 into two subsets such that their difference of sum is minimum

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.