Minimum and Maximum LCM among all pairs (i, j) in range [L, R]
Given two positive integers L and R representing a range. The task is to find the minimum and maximum possible LCM of any pair (i, j) in the range [L, R] such that L ≤ i < j ≤ R.
Input: L = 2, R = 6
Output: 4 30
Explanations: Following are the pairs with minimum and maximum LCM in range [2, 6].
Minimum LCM –> (2, 4) = 4
Maximum LCM –> (5, 6) = 30
Input: L = 5, R = 93
Output: 10 8556
Approach: This problem can be solved by using simple Mathematics. Follow the steps below to solve the given problem.
For Minimum LCM,
- One number would be for sure the minimum number in the range [L, R].
- Choose numbers in such a way that one is a factor of the other.
- The only number with L that gives the minimum LCM is 2*L.
- Check if 2*L <= R
- If Yes, the Minimum LCM would be 2*L
- Otherwise, the Minimum LCM would be L*(L+1).
For Maximum LCM,
- One number would be for sure the maximum number in the range [L, R] that is R.
- Choose a second number such that it is co-prime with R and the product of both is maximum.
- R and R-1 will be always co-prime if R!=2.
- Therefore, R*(R-1) will be giving the maximum LCM.
Below is the implementation of the above approach:
Time Complexity: O(1)
Auxiliary Space: O(1)