Given two numbers L and R, the task is to find two distinct minimum positive integers X and Y such that whose LCM lies in the range [L, R]. If there doesn’t exist any value of X and Y then print “-1”.
Examples:
Input: L = 3, R = 8
Output: x = 3, y=6
Explanation:
LCM of 3 and 6 is 6 which is in range 3, 8Input: L = 88, R = 90
Output: -1
Explanation:
Minimum possible x and y are 88 and 176 respectively, but 176 is greater than 90.
Approach: The idea is to choose the value of X and Y in such a way that their LCM lies in the given range [L, R]. Below are the steps:
- For the minimum value of X choose L as the minimum value as this is the minimum value in the given range.
- Now for the value of Y choose 2*L as this is the minimum value of Y whose LCM is L.
- Now if the above two values of X and Y lie in the range [L, R], then this is required pair of integers with minimum possible values of X and Y.
- Otherwise, print “-1” as there doesn’t exist any other pair.
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 two distinct numbers // X and Y s.t. their LCM lies between // L and R and X, Y are minimum possible void answer(int L, int R) { // Check if 2*L lies in range L, R if (2 * L <= R) // Print the answer cout << L << ", " << 2 * L << "\n"; else cout << -1; } // Driver Code int main() { // Given value of ranges int L = 3, R = 8; // Function call answer(L, R); return 0; } |
Java
// Java program for the above approach import java.io.*; class GFG{ // Function to find two distinct numbers // X and Y s.t. their LCM lies between // L and R and X, Y are minimum possible static void answer(int L, int R) { // Check if 2*L lies in range L, R if (2 * L <= R) // Print the answer System.out.println(L + ", " + (2 * L)); else System.out.println("-1"); } // Driver Code public static void main(String[] args) { // Given value of ranges int L = 3, R = 8; // Function call answer(L, R); } } // This code is contributed by sanjoy_62 |
Python3
# Python3 program for the above approach # Function to find two distinct numbers # X and Y s.t. their LCM lies between # L and R and X, Y are minimum possible def answer(L, R): # Check if 2*L lies in range L, R if (2 * L <= R): # Print the answer print(L, ",", 2 * L) else: print(-1) # Driver Code # Given value of ranges L = 3R = 8 # Function call answer(L, R) # This code is contributed by sanjoy_62 |
C#
// C# program for the above approach using System; class GFG{ // Function to find two distinct numbers // X and Y s.t. their LCM lies between // L and R and X, Y are minimum possible static void answer(int L, int R) { // Check if 2*L lies in range L, R if (2 * L <= R) // Print the answer Console.WriteLine(L + ", " + (2 * L)); else Console.WriteLine("-1"); } // Driver Code public static void Main() { // Given value of ranges int L = 3, R = 8; // Function call answer(L, R); } } // This code is contributed by sanjoy_62 |
3, 6
Time Complexity: O(1)
Auxiliary Space: O(1)
Recommended Posts:
- Sum of LCM(1, n), LCM(2, n), LCM(3, n), ... , LCM(n, n)
- Generate a pair of integers from a range [L, R] whose LCM also lies within the range
- Minimum replacement of pairs by their LCM required to reduce given array to its LCM
- Maximum sum of distinct numbers such that LCM of these numbers is N
- Maximum time such that absolute difference between hour and minute lies in given range
- Find three integers less than or equal to N such that their LCM is maximum
- Find the pair (a, b) with minimum LCM such that their sum is equal to N
- Count of pairs in a given range with sum of their product and sum equal to their concatenated number
- Find two numbers made up of a given digit such that their difference is divisible by N
- Number of pairs such that their HCF and LCM is equal
- Count of pairs upto N such whose LCM is not equal to their product for Q queries
- Find the GCD that lies in given range
- Find any K distinct odd integers such that their sum is equal to N
- Choose points from two ranges such that no point lies in both the ranges
- Find two numbers such that difference of their squares equal to N
- Count pairs from 1 to N such that their Sum is divisible by their XOR
- Possible values of Q such that, for any value of R, their product is equal to X times their sum
- Numbers in range [L, R] such that the count of their divisors is both even and prime
- Find two numbers with the given LCM and minimum possible difference
- Find two numbers with given sum and maximum possible LCM
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.
Improved By : sanjoy_62


