Given an integer **X**, the task is to find two integers **A **and **B** such that sum of these two numbers is **X** and the LCM of **A** and **B** is maximum.

**Examples:**

Input:X = 15Output:7 8Explanation:

7 + 8 = 15 and LCM(7, 8) = 56 is the maximum possible.

Input:X = 30Output:13 17Explanation:

13 + 17 = 30 and LCM(13, 17) = 221 is the maximum possible.

**Naive Approach: **The simplest approach is to use Two Pointers to find the pair of integers **A** and **B **with a given sum **X** and maximum possible *LCM*. Below are the steps:

- Initialize
**A**and**B**as*1*and**X**–*1*respectively. - Run a loop, while,
**A**is less than and equal to**B**. - At each iteration calculate the
*LCM*of**A**and**B,**then increment**A**by*1*and decrement**B**by*1*. - Print the
**A**and**B**corresponding to the maximum*LCM.*

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

**Efficient Approach: **To optimize the above naive approach the idea is to use some mathematical observations. The *LCM* of two co-prime integers is equal to the product of the two integers. Thus, the problem can be simplified to finding two co-prime integers **A** and **B** such that** A+B **= **X **and **A×B** is maximum. Below are the steps:

- If
**X**is odd, then**A =**and**floor(X/2)****B =**.**floor(X/2) + 1** - Otherwise, if
**X**is even, then- If
*floor(*is even, then**X**/2)**A =**and**floor(X/2) – 1****B =****floor(X/2) + 1**. *Otherwise, if floor(*is odd, then**X**/2)**A =**and**floor(X/2) – 2****B =****floor(X/2) + 2**.

- If

Below is the implementation of the above approach:

## C++

`// C++ program of the above approach` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// Function that print two numbers with` `// the sum X and maximum possible LCM` `void` `maxLCMWithGivenSum(` `int` `X)` `{` ` ` `// variables to store the result` ` ` `int` `A, B;` ` ` `// If X is odd` ` ` `if` `(X & 1) {` ` ` `A = X / 2;` ` ` `B = X / 2 + 1;` ` ` `}` ` ` `// If X is even` ` ` `else` `{` ` ` `// If floor(X/2) is even` ` ` `if` `((X / 2) % 2 == 0) {` ` ` `A = X / 2 - 1;` ` ` `B = X / 2 + 1;` ` ` `}` ` ` `// If floor(X/2) is odd` ` ` `else` `{` ` ` `A = X / 2 - 2;` ` ` `B = X / 2 + 2;` ` ` `}` ` ` `}` ` ` `// Print the result` ` ` `cout << A << ` `" "` `<< B << endl;` `}` `// Driver Code` `int` `main()` `{` ` ` `// Given Number` ` ` `int` `X = 30;` ` ` `// Function call` ` ` `maxLCMWithGivenSum(X);` ` ` `return` `0;` `}` |

*chevron_right*

*filter_none*

## Java

`// Java program of the above approach ` `import` `java.util.*;` `class` `GFG{` `// Function that print two numbers with` `// the sum X and maximum possible LCM` `static` `void` `maxLCMWithGivenSum(` `int` `X)` `{` ` ` ` ` `// Variables to store the result` ` ` `int` `A, B;` ` ` `// If X is odd` ` ` `if` `((X & ` `1` `) == ` `1` `)` ` ` `{` ` ` `A = X / ` `2` `;` ` ` `B = X / ` `2` `+ ` `1` `;` ` ` `}` ` ` `// If X is even` ` ` `else` ` ` `{` ` ` ` ` `// If floor(X/2) is even` ` ` `if` `((X / ` `2` `) % ` `2` `== ` `0` `)` ` ` `{` ` ` `A = X / ` `2` `- ` `1` `;` ` ` `B = X / ` `2` `+ ` `1` `;` ` ` `}` ` ` `// If floor(X/2) is odd` ` ` `else` ` ` `{` ` ` `A = X / ` `2` `- ` `2` `;` ` ` `B = X / ` `2` `+ ` `2` `;` ` ` `}` ` ` `}` ` ` ` ` `// Print the result` ` ` `System.out.println(A + ` `" "` `+ B); ` `}` `// Driver code` `public` `static` `void` `main(String[] args)` `{` ` ` ` ` `// Given number` ` ` `int` `X = ` `30` `;` ` ` `// Function call` ` ` `maxLCMWithGivenSum(X); ` `}` `}` `// This code is contributed by offbeat` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program for the above approach ` `# Function that print two numbers with ` `# the sum X and maximum possible LCM ` `def` `maxLCMWithGivenSum(X):` ` ` ` ` `# If X is odd ` ` ` `if` `X ` `%` `2` `!` `=` `0` `:` ` ` `A ` `=` `X ` `/` `2` ` ` `B ` `=` `X ` `/` `2` `+` `1` ` ` ` ` `# If X is even ` ` ` `else` `:` ` ` ` ` `# If floor(X/2) is even ` ` ` `if` `(X ` `/` `2` `) ` `%` `2` `=` `=` `0` `:` ` ` `A ` `=` `X ` `/` `2` `-` `1` ` ` `B ` `=` `X ` `/` `2` `+` `1` ` ` ` ` `# If floor(X/2) is odd ` ` ` `else` `:` ` ` `A ` `=` `X ` `/` `2` `-` `2` ` ` `B ` `=` `X ` `/` `2` `+` `2` ` ` ` ` `# Print the result` ` ` `print` `(` `int` `(A), ` `int` `(B), end ` `=` `" "` `)` `# Driver Code ` `if` `__name__ ` `=` `=` `'__main__'` `:` ` ` ` ` `# Given Number` ` ` `X ` `=` `30` ` ` ` ` `# Function call` ` ` `maxLCMWithGivenSum(X)` `# This code is contributed by virusbuddah_` |

*chevron_right*

*filter_none*

## C#

`// C# program of the above approach ` `using` `System;` `class` `GFG{` `// Function that print two numbers with` `// the sum X and maximum possible LCM` `static` `void` `maxLCMWithGivenSum(` `int` `X)` `{` ` ` ` ` `// Variables to store the result` ` ` `int` `A, B;` ` ` `// If X is odd` ` ` `if` `((X & 1) == 1)` ` ` `{` ` ` `A = X / 2;` ` ` `B = X / 2 + 1;` ` ` `}` ` ` `// If X is even` ` ` `else` ` ` `{` ` ` ` ` `// If floor(X/2) is even` ` ` `if` `((X / 2) % 2 == 0)` ` ` `{` ` ` `A = X / 2 - 1;` ` ` `B = X / 2 + 1;` ` ` `}` ` ` `// If floor(X/2) is odd` ` ` `else` ` ` `{` ` ` `A = X / 2 - 2;` ` ` `B = X / 2 + 2;` ` ` `}` ` ` `}` ` ` ` ` `// Print the result` ` ` `Console.WriteLine(A + ` `" "` `+ B); ` `}` `// Driver code` `public` `static` `void` `Main(String[] args)` `{` ` ` ` ` `// Given number` ` ` `int` `X = 30;` ` ` `// Function call` ` ` `maxLCMWithGivenSum(X); ` `}` `}` `// This code is contributed by sapnasingh4991` |

*chevron_right*

*filter_none*

**Output:**

13 17

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