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.
Examples:
Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.
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) = 30Input: 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:
C++
// C++ program for the above approach#include <bits/stdc++.h>using namespace std; // Function to find minimum LCM in range [L, R]int minLCM(int L, int R){ // If 2*L is within the the range // then minimum LCM would be 2*L if (2 * L <= R) return 2 * L; // Otherwise L * (L+1) would give // the minimum LCM else return L * (L + 1);} // Function to find maximum LCM in range [L, R]int maxLCM(int L, int R){ // The only possible equation that will give // the maximum LCM is R * (R-1) return R * (R - 1);} // Driver Codeint main(){ int L = 2; int R = 6; cout << minLCM(L, R) << ' '; cout << maxLCM(L, R);} |
4 30
Time Complexity: O(1)
Auxiliary Space: O(1)

