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:
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:
C++
#include <bits/stdc++.h>
using namespace std;
int minLCM( int L, int R)
{
if (2 * L <= R)
return 2 * L;
else
return L * (L + 1);
}
int maxLCM( int L, int R)
{
return R * (R - 1);
}
int main()
{
int L = 2;
int R = 6;
cout << minLCM(L, R) << ' ' ;
cout << maxLCM(L, R);
}
|
Java
class GFG {
public static int minLCM( int L, int R) {
if ( 2 * L <= R)
return 2 * L;
else
return L * (L + 1 );
}
public static int maxLCM( int L, int R) {
return R * (R - 1 );
}
public static void main(String args[]) {
int L = 2 ;
int R = 6 ;
System.out.print(minLCM(L, R) + " " );
System.out.print(maxLCM(L, R));
}
}
|
Python3
def minLCM(L, R):
if ( 2 * L < = R):
return 2 * L
else :
return L * (L + 1 )
def maxLCM(L, R):
return R * (R - 1 );
if __name__ = = "__main__" :
L = 2 ;
R = 6 ;
print (minLCM(L, R),end = ' ' )
print (maxLCM(L, R))
|
C#
using System;
class GFG
{
public static int minLCM( int L, int R)
{
if (2 * L <= R)
return 2 * L;
else
return L * (L + 1);
}
public static int maxLCM( int L, int R)
{
return R * (R - 1);
}
public static void Main()
{
int L = 2;
int R = 6;
Console.Write(minLCM(L, R) + " " );
Console.Write(maxLCM(L, R));
}
}
|
Javascript
<script>
function minLCM(L, R) {
if (2 * L <= R)
return 2 * L;
else
return L * (L + 1);
}
function maxLCM(L, R) {
return R * (R - 1);
}
let L = 2;
let R = 6;
document.write(minLCM(L, R) + ' ' );
document.write(maxLCM(L, R));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)