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.
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:
- Select the value of x as L and compute the value of y as 2*x
- Check if y is less than R or not.
- If y is less than R then print the pair (x, y)
- Else print “-1”
Below is the implementation of the above approach:
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.
- Sum of LCM(1, n), LCM(2, n), LCM(3, n), ... , LCM(n, n)
- Rearrange two given arrays such that sum of same indexed elements lies within given range
- Find two distinct numbers such that their LCM lies in given range
- Print any pair of integers with sum of GCD and LCM equals to N
- Count all prime numbers in a given range whose sum of digits is also prime
- Largest square that can be inscribed within a hexagon which is inscribed within an equilateral triangle
- Biggest Reuleaux Triangle within a Square which is inscribed within a Circle
- Biggest Reuleaux Triangle within a Square which is inscribed within a Right angle Triangle
- Largest right circular cylinder that can be inscribed within a cone which is in turn inscribed within a cube
- Biggest Reuleaux Triangle inscribed within a square which is inscribed within an ellipse
- Largest right circular cone that can be inscribed within a sphere which is inscribed within a cube
- Biggest Reuleaux Triangle inscribed within a square which is inscribed within a hexagon
- Largest sphere that can be inscribed within a cube which is in turn inscribed within a right circular cone
- Largest ellipse that can be inscribed within a rectangle which in turn is inscribed within a semicircle
- Minimum replacement of pairs by their LCM required to reduce given array to its LCM
- Count number of subsets whose median is also present in the same subset
- Count prime pairs whose difference is also a Prime Number
- Count N-digit numbers made up of X or Y whose sum of digits is also made up of X or Y
- Generate Bitonic Sequence of length N from integers in a given range
- Find three integers less than or equal to N such that their LCM is maximum
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.