Generate a pair of integers from a range [L, R] whose LCM also lies within the range

Given two integers L and R, the task is to find a pair of integers from the range [L, R] having LCM within the range [L, R] as well. If no such pair can be obtained, then print -1. If multiple pairs exist, print any one of them.

Examples:

Input: L = 13, R = 69
Output: X =13, Y = 26
Explanation: LCM(x, y) = 26 which satisfies the conditions L ≤ x < y ≤ R and L <= LCM(x, y) <= R

Input: L = 1, R = 665
Output: X = 1, Y = 2

Naive Approach: The simplest approach is to generate every pair between L and R and compute their LCM. Print a pair having LCM between the range L and R. If no pair is found to have LCM in the given range, print “-1”.



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

Efficient Approach: The problem can be solved by using the Greedy technique based on the observation that LCM(x, y) is at least equal to 2*x which is LCM of (x, 2*x). Below are the steps to implement the approach:

  1. Select the value of x as L and compute the value of y as 2*x
  2. Check if y is less than R or not.
  3. If y is less than R then print the pair (x, y)
  4. Else print “-1”

Below is the implementation of the above approach:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the above approach
#include <iostream>
using namespace std;
 
void lcmpair(int l, int r)
{
    int x, y;
    x = l;
    y = 2 * l;
 
    // Checking if any pair is possible
    // or not in range(l, r)
    if (y > r) {
 
        // If not possible print(-1)
        cout << "-1\n";
    }
    else {
 
        // Print LCM pair
        cout << "X = " << x << " Y = "
             << y << "\n";
    }
}
 
// Driver code
int main()
{
    int l = 13, r = 69;
 
    // Function call
    lcmpair(l, r);
    return 0;
}
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the above approach
import java.util.*;
 
class GFG{
 
static void lcmpair(int l, int r)
{
    int x, y;
    x = l;
    y = 2 * l;
 
    // Checking if any pair is possible
    // or not in range(l, r)
    if (y > r)
    {
         
        // If not possible print(-1)
        System.out.print("-1\n");
    }
    else
    {
         
        // Print LCM pair
        System.out.print("X = " + x +
                        " Y = " + y + "\n");
    }
}
 
// Driver code
public static void main(String[] args)
{
    int l = 13, r = 69;
 
    // Function call
    lcmpair(l, r);
}
}
 
// This code is contributed by 29AjayKumar
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the above approach
def lcmpair(l, r):
 
    x = l
    y = 2 * l
 
    # Checking if any pair is possible
    # or not in range(l, r)
    if(y > r):
 
        # If not possible print(-1)
        print(-1)
    else:
         
        # Print LCM pair
        print("X = {} Y = {}".format(x, y))
 
# Driver Code
l = 13
r = 69
 
# Function call
lcmpair(l, r)
 
# This code is contributed by Shivam Singh
chevron_right

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the above approach
using System;
class GFG{
 
static void lcmpair(int l, int r)
{
    int x, y;
    x = l;
    y = 2 * l;
 
    // Checking if any pair is possible
    // or not in range(l, r)
    if (y > r)
    {       
        // If not possible print(-1)
        Console.Write("-1\n");
    }
    else
    {       
        // Print LCM pair
        Console.Write("X = " + x +
                      " Y = " + y + "\n");
    }
}
 
// Driver code
public static void Main(String[] args)
{
    int l = 13, r = 69;
 
    // Function call
    lcmpair(l, r);
}
}
 
// This code is contributed by shikhasingrajput
chevron_right

Output: 
X = 13 Y = 26



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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




Recommended Posts:


Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Article Tags :