Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Minimum and Maximum LCM among all pairs (i, j) in range [L, R]

  • Last Updated : 11 Nov, 2021

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++




// 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 Code
int main()
{
    int L = 2;
    int R = 6;
 
    cout << minLCM(L, R) << ' ';
    cout << maxLCM(L, R);
}


Java




// Java program for the above approach
 
class GFG {
 
    // Function to find minimum LCM in range [L, R]
    public static 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]
    public static 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 Code
    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




# Python program for the above approach
 
# Function to find minimum LCM in range [L, R]
def minLCM(L, 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]
def maxLCM(L, R):
 
  # The only possible equation that will give
  # the maximum LCM is R * (R-1)
  return R * (R - 1);
 
# Driver Code
if __name__=="__main__":
   
  L = 2;
  R = 6;
 
  print(minLCM(L, R),end=' ')
  print(maxLCM(L, R))
 
#This code is contributed by Akash Jha


C#




// C# program for the above approach
using System;
class GFG
{
 
    // Function to find minimum LCM in range [L, R]
    public static 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]
    public static 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 Code
    public static void Main()
    {
        int L = 2;
        int R = 6;
 
        Console.Write(minLCM(L, R) + " ");
        Console.Write(maxLCM(L, R));
    }
}


Javascript




<script>
// Javascript program for the above approach
 
 
// Function to find minimum LCM in range [L, R]
function minLCM(L, 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]
function maxLCM(L, R) {
 
  // The only possible equation that will give
  // the maximum LCM is R * (R-1)
  return R * (R - 1);
}
 
// Driver Code
 
let L = 2;
let R = 6;
 
document.write(minLCM(L, R) + ' ');
document.write(maxLCM(L, R));
</script>


Output

4 30

Time Complexity: O(1) 
Auxiliary Space: O(1)


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!