18. LCM Triplet
β GFG solution to the LCM Triplet problem: find maximum possible LCM by selecting three numbers β€ n using mathematical optimization and greedy approach. π
The problem can be found at the following link: π Question Link
π§© Problem Description
Given a number n
, find the maximum possible LCM that can be obtained by selecting three numbers less than or equal to n
.
Note: LCM stands for Lowest Common Multiple.
The goal is to find three numbers β€ n such that their LCM is maximized.
π Examples
Example 1
Input: n = 9
Output: 504
Explanation: 504 is the maximum LCM that can be attained by any triplet of numbers less than or equal 9.
The triplet which has this LCM is {7, 8, 9}.
Example 2
Input: n = 7
Output: 210
Explanation: 210 is the maximum LCM that can be attained by any triplet of numbers less than or equal 7.
The triplet which has this LCM is {5, 6, 7}.
π Constraints
$1 \le n \le 10^3$
β
My Approach
The optimal approach uses Mathematical Optimization with Greedy Strategy to maximize LCM:
Mathematical Analysis + Greedy Selection
Key Insight:
To maximize LCM, we need to choose numbers that are as large as possible and have minimal common factors.
The LCM of three numbers is maximized when they are pairwise coprime (gcd = 1).
Case Analysis:
Case 1:
n < 3
β Returnn
(insufficient numbers for triplet)Case 2:
n
is odd β Choose{n, n-1, n-2}
(consecutive numbers have small gcds)Case 3:
n
is even andn % 3 β 0
β Choose{n, n-1, n-3}
(avoid n-2 which is even)Case 4:
n
is even andn % 3 = 0
β Choose{n-1, n-2, n-3}
(avoid n which is divisible by 3)
Mathematical Reasoning:
When
n
is odd:n
andn-1
are coprime,n-1
andn-2
are coprimeWhen
n
is even: avoid consecutive even numbers to minimize common factorsWhen
n % 3 = 0
: avoidn
to prevent factor of 3 from reducing LCM
Optimization:
Use bitwise operations for faster even/odd checks
Minimize conditional branches with ternary operators
Perform constant-time calculations regardless of input size
π Time and Auxiliary Space Complexity
Expected Time Complexity: O(1), as we perform a constant number of arithmetic operations and conditional checks regardless of the input value n.
Expected Auxiliary Space Complexity: O(1), as we only use a constant amount of additional space for storing intermediate calculations.
π§βπ» Code (C++)
class Solution {
public:
int lcmTriplets(int n) {
return n < 3 ? n : n & 1 ? n * (n - 1) * (n - 2) :
n % 3 ? n * (n - 1) * (n - 3) : (n - 1) * (n - 2) * (n - 3);
}
};
π§βπ» Code (Java)
class Solution {
int lcmTriplets(int n) {
return n < 3 ? n : (n & 1) == 1 ? n * (n - 1) * (n - 2) :
n % 3 != 0 ? n * (n - 1) * (n - 3) : (n - 1) * (n - 2) * (n - 3);
}
}
π Code (Python)
class Solution:
def lcmTriplets(self, n):
return n if n < 3 else n * (n - 1) * (n - 2) if n & 1 else \
n * (n - 1) * (n - 3) if n % 3 else (n - 1) * (n - 2) * (n - 3)
π§ Contribution and Support
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: π¬ Any Questions?. Let's make this learning journey more collaborative!
β If you find this helpful, please give this repository a star! β
πVisitor Count
Last updated