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 = 3Output:1 2 3Input:N = 5Output: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-3will be required answer.

Below is the implementation of the above approach:

## C++

`// CPP Program to find three integers ` `// less than N whose LCM is maximum ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// function to find three integers ` `// less than N whose LCM is maximum ` `void` `MaxLCM(` `int` `n) ` `{ ` ` ` `// if n is odd ` ` ` `if` `(n % 2 != 0) ` ` ` `cout << n << ` `" "` `<< (n - 1) << ` `" "` `<< (n - 2); ` ` ` ` ` `// if n is even and n, n-3 gcd is 1 ` ` ` `else` `if` `(__gcd(n, (n - 3)) == 1) ` ` ` `cout << n << ` `" "` `<< (n - 1) << ` `" "` `<< (n - 3); ` ` ` ` ` `else` ` ` `cout << (n - 1) << ` `" "` `<< (n - 2) << ` `" "` `<< (n - 3); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `n = 12; ` ` ` ` ` `// function call ` ` ` `MaxLCM(n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java Program to find three integers ` `// less than N whose LCM is maximum ` ` ` `import` `java.io.*; ` ` ` `class` `GFG { ` ` ` `// Recursive function to return gcd of a and b ` `static` `int` `__gcd(` `int` `a, ` `int` `b) ` `{ ` ` ` `// Everything divides 0 ` ` ` `if` `(a == ` `0` `) ` ` ` `return` `b; ` ` ` `if` `(b == ` `0` `) ` ` ` `return` `a; ` ` ` ` ` `// base case ` ` ` `if` `(a == b) ` ` ` `return` `a; ` ` ` ` ` `// a is greater ` ` ` `if` `(a > b) ` ` ` `return` `__gcd(a-b, b); ` ` ` `return` `__gcd(a, b-a); ` `} ` ` ` `// function to find three integers ` `// less than N whose LCM is maximum ` `static` `void` `MaxLCM(` `int` `n) ` `{ ` ` ` `// if n is odd ` ` ` `if` `(n % ` `2` `!= ` `0` `) ` ` ` `System.out.print(n + ` `" "` `+ (n - ` `1` `) + ` `" "` `+ (n - ` `2` `)); ` ` ` ` ` `// if n is even and n, n-3 gcd is 1 ` ` ` `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` `)); ` `} ` ` ` `// Driver code ` `public` `static` `void` `main (String[] args) { ` ` ` `int` `n = ` `12` `; ` ` ` ` ` `// function call ` ` ` `MaxLCM(n); ` ` ` `} ` `} ` `// This code is contributed by anuj_67.. ` |

*chevron_right*

*filter_none*

## Python3

`# Python 3 Program to find three integers ` `# less than N whose LCM is maximum ` `from` `math ` `import` `gcd ` ` ` `# function to find three integers ` `# less than N whose LCM is maximum ` `def` `MaxLCM(n) : ` ` ` ` ` `# if n is odd ` ` ` `if` `(n ` `%` `2` `!` `=` `0` `) : ` ` ` `print` `(n, (n ` `-` `1` `), (n ` `-` `2` `)) ` ` ` ` ` `# if n is even and n, n-3 gcd is 1 ` ` ` `elif` `(gcd(n, (n ` `-` `3` `)) ` `=` `=` `1` `) : ` ` ` `print` `(n, (n ` `-` `1` `), (n ` `-` `3` `)) ` ` ` ` ` `else` `: ` ` ` `print` `((n ` `-` `1` `), (n ` `-` `2` `), (n ` `-` `3` `)) ` ` ` `# Driver Code ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` ` ` `n ` `=` `12` ` ` ` ` `# function call ` ` ` `MaxLCM(n) ` ` ` `# This code is contributed by Ryuga ` |

*chevron_right*

*filter_none*

## C#

`// C# Program to find three integers ` `// less than N whose LCM is maximum ` ` ` `using` `System; ` ` ` `class` `GFG { ` `// Recursive function to return gcd of a and b ` `static` `int` `__gcd(` `int` `a, ` `int` `b) ` `{ ` ` ` `// Everything divides 0 ` ` ` `if` `(a == 0) ` ` ` `return` `b; ` ` ` `if` `(b == 0) ` ` ` `return` `a; ` ` ` ` ` `// base case ` ` ` `if` `(a == b) ` ` ` `return` `a; ` ` ` ` ` `// a is greater ` ` ` `if` `(a > b) ` ` ` `return` `__gcd(a-b, b); ` ` ` `return` `__gcd(a, b-a); ` `} ` ` ` `// function to find three integers ` `// less than N whose LCM is maximum ` `static` `void` `MaxLCM(` `int` `n) ` `{ ` ` ` `// if n is odd ` ` ` `if` `(n % 2 != 0) ` ` ` `Console.Write(n + ` `" "` `+ (n - 1) + ` `" "` `+ (n - 2)); ` ` ` ` ` `// if n is even and n, n-3 gcd is 1 ` ` ` `else` `if` `(__gcd(n, (n - 3)) == 1) ` ` ` `Console.Write( n + ` `" "` `+(n - 1)+ ` `" "` `+ (n - 3)); ` ` ` ` ` `else` ` ` `Console.Write((n - 1) + ` `" "` `+ (n - 2) + ` `" "` `+ (n - 3)); ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main () { ` ` ` `int` `n = 12; ` ` ` ` ` `// function call ` ` ` `MaxLCM(n); ` ` ` `} ` `} ` `// This code is contributed by anuj_67.. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP Program to find three integers ` `// less than N whose LCM is maximum ` ` ` `// Recursive function to return ` `// gcd of a and b ` `function` `__gcd(` `$a` `, ` `$b` `) ` `{ ` ` ` `// Everything divides 0 ` ` ` `if` `(` `$a` `== 0) ` ` ` `return` `$b` `; ` ` ` `if` `(` `$b` `== 0) ` ` ` `return` `$a` `; ` ` ` ` ` `// base case ` ` ` `if` `(` `$a` `== ` `$b` `) ` ` ` `return` `$a` `; ` ` ` ` ` `// a is greater ` ` ` `if` `(` `$a` `> ` `$b` `) ` ` ` `return` `__gcd(` `$a` `- ` `$b` `, ` `$b` `); ` ` ` `return` `__gcd(` `$a` `, ` `$b` `- ` `$a` `); ` `} ` ` ` `// function to find three integers ` `// less than N whose LCM is maximum ` `function` `MaxLCM(` `$n` `) ` `{ ` ` ` `// if n is odd ` ` ` `if` `(` `$n` `% 2 != 0) ` ` ` `echo` `$n` `, ` `" "` `, (` `$n` `- 1) , ` ` ` `" "` `, (` `$n` `- 2); ` ` ` ` ` `// if n is even and n, n-3 gcd is 1 ` ` ` `else` `if` `(__gcd(` `$n` `, (` `$n` `- 3)) == 1) ` ` ` `echo` `$n` `, ` `" "` `, (` `$n` `- 1), ` ` ` `" "` `, (` `$n` `- 3); ` ` ` ` ` `else` ` ` `echo` `(` `$n` `- 1) , ` `" "` `, (` `$n` `- 2), ` ` ` `" "` `, (` `$n` `- 3); ` `} ` ` ` `// Driver code ` `$n` `= 12; ` ` ` `// function call ` `MaxLCM(` `$n` `); ` ` ` `// This code is contributed by Sachin ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

11 10 9

## Recommended Posts:

- Sum of LCM(1, n), LCM(2, n), LCM(3, n), ... , LCM(n, n)
- Find the pair (a, b) with minimum LCM such that their sum is equal to N
- Minimum replacement of pairs by their LCM required to reduce given array to its LCM
- 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
- Find the number of positive integers less than or equal to N that have an odd number of digits
- 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
- Sum of elements in 1st array such that number of elements less than or equal to them in 2nd array is maximum
- Possible values of Q such that, for any value of R, their product is equal to X times their sum
- Count number of integers less than or equal to N which has exactly 9 divisors
- Count of integers of length N and value less than K such that they contain digits only from the given set
- Find two distinct numbers such that their LCM lies in given range
- Find unique pairs such that each element is less than or equal to N
- Highest and Smallest power of K less than and greater than equal to N respectively
- Find maximum product of digits among numbers less than or equal to N
- Largest number with maximum trailing nines which is less than N and greater than N-D
- Numbers less than N that are perfect cubes and the sum of their digits reduced to a single digit is 1
- Find K consecutive integers such that their sum is N
- Maximum size of square such that all submatrices of that size have sum less than K

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.