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 = 15
Output: 7 8
Explanation:
7 + 8 = 15 and LCM(7, 8) = 56 is the maximum possible.
Input: X = 30
Output: 13 17
Explanation:
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 = floor(X/2) and B = floor(X/2) + 1.
- Otherwise, if X is even, then
- If floor(X/2) is even, then A = floor(X/2) – 1 and B = floor(X/2) + 1.
- Otherwise, if floor(X/2) is odd, then A = floor(X/2) – 2 and B = floor(X/2) + 2.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void maxLCMWithGivenSum( int X)
{
int A, B;
if (X & 1) {
A = X / 2;
B = X / 2 + 1;
}
else {
if ((X / 2) % 2 == 0) {
A = X / 2 - 1;
B = X / 2 + 1;
}
else {
A = X / 2 - 2;
B = X / 2 + 2;
}
}
cout << A << " " << B << endl;
}
int main()
{
int X = 30;
maxLCMWithGivenSum(X);
return 0;
}
|
Java
import java.util.*;
class GFG{
static void maxLCMWithGivenSum( int X)
{
int A, B;
if ((X & 1 ) == 1 )
{
A = X / 2 ;
B = X / 2 + 1 ;
}
else
{
if ((X / 2 ) % 2 == 0 )
{
A = X / 2 - 1 ;
B = X / 2 + 1 ;
}
else
{
A = X / 2 - 2 ;
B = X / 2 + 2 ;
}
}
System.out.println(A + " " + B);
}
public static void main(String[] args)
{
int X = 30 ;
maxLCMWithGivenSum(X);
}
}
|
Python3
def maxLCMWithGivenSum(X):
if X % 2 ! = 0 :
A = X / 2
B = X / 2 + 1
else :
if (X / 2 ) % 2 = = 0 :
A = X / 2 - 1
B = X / 2 + 1
else :
A = X / 2 - 2
B = X / 2 + 2
print ( int (A), int (B), end = " " )
if __name__ = = '__main__' :
X = 30
maxLCMWithGivenSum(X)
|
C#
using System;
class GFG{
static void maxLCMWithGivenSum( int X)
{
int A, B;
if ((X & 1) == 1)
{
A = X / 2;
B = X / 2 + 1;
}
else
{
if ((X / 2) % 2 == 0)
{
A = X / 2 - 1;
B = X / 2 + 1;
}
else
{
A = X / 2 - 2;
B = X / 2 + 2;
}
}
Console.WriteLine(A + " " + B);
}
public static void Main(String[] args)
{
int X = 30;
maxLCMWithGivenSum(X);
}
}
|
Javascript
<script>
function maxLCMWithGivenSum(X)
{
let A, B;
if (X & 1) {
A = X / 2;
B = X / 2 + 1;
}
else {
if ((X / 2) % 2 == 0) {
A = X / 2 - 1;
B = X / 2 + 1;
}
else {
A = X / 2 - 2;
B = X / 2 + 2;
}
}
document.write(A + " " + B + "<br>" );
}
let X = 30;
maxLCMWithGivenSum(X);
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Approach#2: Using math
This approach used in this code is a brute-force method to find the two numbers with the given sum X that have the maximum possible LCM. The code checks all possible pairs of numbers that sum up to X and calculates their LCM using the math.gcd() function. The code keeps track of the maximum LCM found so far and returns the pair of numbers that gives this maximum LCM.
Algorithm
1. Initialize a variable max_lcm to 0.
2. Loop over all possible pairs of numbers (i, j) such that i < j and i + j = X.
3. Calculate the LCM of i and j using the formula (i*j) // math.gcd(i, j).
4. If the calculated LCM is greater than max_lcm, update max_lcm and the pair of numbers num1 and num2 accordingly.
5. Return the pair of numbers num1 and num2.
C++
#include <iostream>
#include <algorithm>
#include <numeric>
using namespace std;
pair< int , int > findNumbersWithLCM( int X) {
int maxLCM = 0;
pair< int , int > result;
for ( int i = 1; i < X; i++) {
for ( int j = i + 1; j < X; j++) {
if (i + j == X) {
int lcm = (i * j) / __gcd(i, j);
if (lcm > maxLCM) {
maxLCM = lcm;
result = {i, j};
}
}
}
}
return result;
}
int main() {
int X = 30;
pair< int , int > result = findNumbersWithLCM(X);
cout << result.first << " " << result.second << endl;
return 0;
}
|
Java
public class GFG {
public static void main(String[] args)
{
int X = 30 ;
int [] result = findNumbersWithLCM(X);
int num1 = result[ 0 ];
int num2 = result[ 1 ];
System.out.println(num1 + " " + num2);
}
public static int [] findNumbersWithLCM( int X)
{
int maxLCM = 0 ;
int [] result = new int [] { 0 , 0 };
for ( int i = 1 ; i < X; i++) {
for ( int j = i + 1 ; j < X; j++) {
if (i + j == X) {
int lcm = (i * j) / gcd(i, j);
if (lcm > maxLCM) {
maxLCM = lcm;
result[ 0 ] = i;
result[ 1 ] = j;
}
}
}
}
return result;
}
public static int gcd( int a, int b)
{
if (b == 0 ) {
return a;
}
return gcd(b, a % b);
}
}
|
Python3
import math
def find_numbers_with_lcm(X):
max_lcm = 0
for i in range ( 1 , X):
for j in range (i + 1 , X):
if i + j = = X:
lcm = (i * j) / / math.gcd(i, j)
if lcm > max_lcm:
max_lcm = lcm
num1, num2 = i, j
return num1, num2
X = 30
num1, num2 = find_numbers_with_lcm(X)
print (num1, num2)
|
C#
using System;
class MainClass
{
public static Tuple< int , int > FindNumbersWithLCM( int X)
{
int maxLCM = 0;
Tuple< int , int > result = Tuple.Create(0, 0);
for ( int i = 1; i < X; i++)
{
for ( int j = i + 1; j < X; j++)
{
if (i + j == X)
{
int gcd = GCD(i, j);
int lcm = (i * j) / gcd;
if (lcm > maxLCM)
{
maxLCM = lcm;
result = Tuple.Create(i, j);
}
}
}
}
return result;
}
public static int GCD( int a, int b)
{
while (b != 0)
{
int temp = b;
b = a % b;
a = temp;
}
return a;
}
public static void Main( string [] args)
{
int X = 30;
Tuple< int , int > result = FindNumbersWithLCM(X);
Console.WriteLine(result.Item1 + " " + result.Item2);
}
}
|
Javascript
function calculateGCD(a, b) {
while (b !== 0) {
const temp = b;
b = a % b;
a = temp;
}
return a;
}
function findNumbersWithLCM(X) {
let maxLCM = 0;
let num1 = 0;
let num2 = 0;
for (let i = 1; i < X; i++) {
for (let j = i + 1; j < X; j++) {
if (i + j === X) {
const gcd = calculateGCD(i, j);
const lcm = (i * j) / gcd;
if (lcm > maxLCM) {
maxLCM = lcm;
num1 = i;
num2 = j;
}
}
}
}
return [num1, num2];
}
const X = 30;
const [num1, num2] = findNumbersWithLCM(X);
console.log(num1, num2);
|
Time Complexity: O(X^2) because it loops over all possible pairs of numbers (i, j) such that i < j and i + j = X.
Auxiliary Space: O(1) because it uses only a fixed number of variables regardless of the value of X.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
28 Nov, 2023
Like Article
Save Article