Given a range [L, R] and an integer K, the task is to check if it is possible to make the GCD of all integers in the given range more than 1 using at most K operations wherein each operation replace any two integers in the range with their product.
Examples:
Input: L = 1, R = 5, K = 3 .
Output: Yes
Explanation: All the integers in the given range are {1, 2, 3, 4, 5}. In first operation, replace the first two elements with their product. Hence, the remaining elements become {2, 3, 4, 5}. Similarly, {2, 3, 4, 5} => {2, 3, 20} => {2, 60}. Hence, in three operations the given range can be reduced to {2, 60} such that its GCD is greater than 1.
Input: L = 1, R = 2, K = 0
Output: No
Approach: The given problem can be solved using a greedy approach. The idea is to find the most common prime factor among all the integers in the range so that other elements can be merged with them. It can be observed that the most common prime factor in all cases will be 2. Hence, the most optimal choice is to multiply all odd integers with their nearest even integer. Therefore, if the count of odd integers if more than K, it is possible otherwise not.
Below is the implementation of the above approach:
C++
#include <iostream>
using namespace std;
bool gcdGreaterThanOne( int L, int R, int K)
{
if (L == R) {
if (L == 1)
return false ;
else
return true ;
}
int odd = (R - L + 1)
- (R / 2 - ((L - 1) / 2));
return (odd > K) ? false : true ;
}
int main()
{
int L = 1;
int R = 5;
int K = 3;
if (gcdGreaterThanOne(L, R, K))
cout << "Yes" ;
else
cout << "No" ;
return 0;
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
static Boolean gcdGreaterThanOne( int L, int R, int K)
{
if (L == R) {
if (L == 1 )
return false ;
else
return true ;
}
int odd = (R - L + 1 )
- (R / 2 - ((L - 1 ) / 2 ));
return (odd > K) ? false : true ;
}
public static void main (String[] args) {
int L = 1 ;
int R = 5 ;
int K = 3 ;
if (gcdGreaterThanOne(L, R, K))
System.out.println( "Yes" );
else
System.out.println( "No" );
}
}
|
Python3
def gcdGreaterThanOne(L, R, K):
if (L = = R):
if (L = = 1 ):
return false
else :
return true
odd = (R - L + 1 ) - (R / 2 - ((L - 1 ) / 2 ))
return (odd < = K)
L = 1
R = 5
K = 3
if (gcdGreaterThanOne(L, R, K)):
print ( "Yes" )
else :
print ( "No" )
|
C#
using System;
class GFG{
static Boolean gcdGreaterThanOne( int L, int R, int K)
{
if (L == R) {
if (L == 1)
return false ;
else
return true ;
}
int odd = (R - L + 1)
- (R / 2 - ((L - 1) / 2));
return (odd > K) ? false : true ;
}
static public void Main (){
int L = 1;
int R = 5;
int K = 3;
if (gcdGreaterThanOne(L, R, K))
Console.Write( "Yes" );
else
Console.Write( "No" );
}
}
|
Javascript
<script>
function gcdGreaterThanOne(L, R, K)
{
if (L == R) {
if (L == 1)
return false ;
else
return true ;
}
let odd = (R - L + 1)
- (R / 2 - ((L - 1) / 2));
return (odd > K) ? false : true ;
}
let L = 1;
let R = 5;
let K = 3;
if (gcdGreaterThanOne(L, R, K))
document.write( "Yes" );
else
document.write( "No" );
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)