Find two distinct numbers such that their LCM lies in given range
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, 8
Input: 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++
#include <bits/stdc++.h>
using namespace std;
void answer( int L, int R)
{
if (2 * L <= R)
cout << L << ", "
<< 2 * L << "\n" ;
else
cout << -1;
}
int main()
{
int L = 3, R = 8;
answer(L, R);
return 0;
}
|
Java
import java.io.*;
class GFG{
static void answer( int L, int R)
{
if ( 2 * L <= R)
System.out.println(L + ", " + ( 2 * L));
else
System.out.println( "-1" );
}
public static void main(String[] args)
{
int L = 3 , R = 8 ;
answer(L, R);
}
}
|
Python3
def answer(L, R):
if ( 2 * L < = R):
print (L, "," , 2 * L)
else :
print ( - 1 )
L = 3
R = 8
answer(L, R)
|
C#
using System;
class GFG{
static void answer( int L, int R)
{
if (2 * L <= R)
Console.WriteLine(L + ", " + (2 * L));
else
Console.WriteLine( "-1" );
}
public static void Main()
{
int L = 3, R = 8;
answer(L, R);
}
}
|
Javascript
<script>
function answer(L, R)
{
if (2 * L <= R)
document.write(L + ", "
+ 2 * L + "<br>" );
else
document.write(-1);
}
let L = 3, R = 8;
answer(L, R);
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Last Updated :
31 Jan, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...