Find three integers less than or equal to N such that their LCM is maximum
Last Updated :
08 Jul, 2022
Given a number N(>=3). The task is to find the three integers (<=N) such that LCM of these three integers is maximum.
Examples:
Input: N = 3
Output: 1 2 3
Input: N = 5
Output: 3 4 5
Approach: Since the task is to maximize the LCM, so if all three numbers don’t have any common factor then the LCM will be the product of those three numbers and that will be maximum.
- If n is odd then the answer will be n, n-1, n-2.
- If n is even,
- If gcd of n and n-3 is 1 then answer will be n, n-1, n-3.
- Otherwise, n-1, n-2, n-3 will be required answer.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
void MaxLCM( int n)
{
if (n % 2 != 0)
cout << n << " " << (n - 1) << " " << (n - 2);
else if (__gcd(n, (n - 3)) == 1)
cout << n << " " << (n - 1) << " " << (n - 3);
else
cout << (n - 1) << " " << (n - 2) << " " << (n - 3);
}
int main()
{
int n = 12;
MaxLCM(n);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int __gcd( int a, int b)
{
if (a == 0 )
return b;
if (b == 0 )
return a;
if (a == b)
return a;
if (a > b)
return __gcd(a-b, b);
return __gcd(a, b-a);
}
static void MaxLCM( int n)
{
if (n % 2 != 0 )
System.out.print(n + " " + (n - 1 ) + " " + (n - 2 ));
else if (__gcd(n, (n - 3 )) == 1 )
System.out.print( n + " " +(n - 1 )+ " " + (n - 3 ));
else
System.out.print((n - 1 ) + " " + (n - 2 ) + " " + (n - 3 ));
}
public static void main (String[] args) {
int n = 12 ;
MaxLCM(n);
}
}
|
Python3
from math import gcd
def MaxLCM(n) :
if (n % 2 ! = 0 ) :
print (n, (n - 1 ), (n - 2 ))
elif (gcd(n, (n - 3 )) = = 1 ) :
print (n, (n - 1 ), (n - 3 ))
else :
print ((n - 1 ), (n - 2 ), (n - 3 ))
if __name__ = = "__main__" :
n = 12
MaxLCM(n)
|
C#
using System;
class GFG {
static int __gcd( int a, int b)
{
if (a == 0)
return b;
if (b == 0)
return a;
if (a == b)
return a;
if (a > b)
return __gcd(a-b, b);
return __gcd(a, b-a);
}
static void MaxLCM( int n)
{
if (n % 2 != 0)
Console.Write(n + " " + (n - 1) + " " + (n - 2));
else if (__gcd(n, (n - 3)) == 1)
Console.Write( n + " " +(n - 1)+ " " + (n - 3));
else
Console.Write((n - 1) + " " + (n - 2) + " " + (n - 3));
}
public static void Main () {
int n = 12;
MaxLCM(n);
}
}
|
PHP
<?php
function __gcd( $a , $b )
{
if ( $a == 0)
return $b ;
if ( $b == 0)
return $a ;
if ( $a == $b )
return $a ;
if ( $a > $b )
return __gcd( $a - $b , $b );
return __gcd( $a , $b - $a );
}
function MaxLCM( $n )
{
if ( $n % 2 != 0)
echo $n , " " , ( $n - 1) ,
" " , ( $n - 2);
else if (__gcd( $n , ( $n - 3)) == 1)
echo $n , " " , ( $n - 1),
" " , ( $n - 3);
else
echo ( $n - 1) , " " , ( $n - 2),
" " , ( $n - 3);
}
$n = 12;
MaxLCM( $n );
?>
|
Javascript
<script>
function __gcd(a , b)
{
if (a == 0)
return b;
if (b == 0)
return a;
if (a == b)
return a;
if (a > b)
return __gcd(a - b, b);
return __gcd(a, b - a);
}
function MaxLCM(n) {
if (n % 2 != 0)
document.write(n + " " + (n - 1) + " " + (n - 2));
else if (__gcd(n, (n - 3)) == 1)
document.write(n + " " + (n - 1) + " " + (n - 3));
else
document.write((n - 1) + " " + (n - 2) + " " + (n - 3));
}
var n = 12;
MaxLCM(n);
</script>
|
Time Complexity: O(log(min(a, b))), where a and b are two parameters of gcd.
Auxiliary Space: O(log(min(a, b)))
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...