Check if GCD of Array can be made greater than 1 by replacing pairs with their products
Last Updated :
09 Feb, 2022
Given three integers L, R, and K. Consider an array arr[] consisting of all the elements from L to R, the task is to check whether the GCD of the array can be made greater than 1 using at most K operations. An operation is defined below:
- Choose any two numbers from the array
- Remove them from the array
- Insert their product back into the array
Examples:
Input: L = 4, R = 10, K = 3
Output: true
Explanation: Array will be arr[] = {4, 5, 6, 7, 8, 9, 10}
Choose arr[0], arr[1]: arr[] = {20, 6, 7, 8, 9, 10}
Choose arr[1], arr[2]: arr[] = {20, 42, 8, 9, 10}
Choose arr[2], arr[3]: arr[] = {20, 42, 72, 10}
GCD of the formed array = 2
Input: L = 3, R = 5, K = 1
Output: false
Explanation: Array will be arr[] = {3, 4, 5}]
Operation on arr[0], arr[1]: arr[] = {12, 5}, GCD = 1, or
Operation on arr[1], arr[2]: arr[] = {3, 20}, GCD = 1, or
Operation on arr[0], arr[2]: arr[] = {4, 15}, GCD = 1
Approach: The task can be solved by converting all the odd array elements to even so that the overall GCD of the array becomes even i.e greater than 1. To check if it is possible or not follow the cases below:
- Case 1: if L = R = 1 then GCD will always be 1, return false
- Case 2: if L = R (and L≠1) then GCD = L, return true
- Case 3: if K is greater equal to the number of odds between range L and R then return true
- If any of the above cases didn’t imply then return false.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
bool gcdOfArray( int L, int R, int K)
{
int range = (R - L + 1);
int even = 0;
int odd = 0;
if (range % 2 == 0) {
even = range / 2;
odd = range - even;
}
else {
if (L % 2 != 0 || R % 2 != 0) {
odd = (range / 2) + 1;
even = range - odd;
}
else {
odd = range / 2;
even = range - odd;
}
}
if (L == R && L == 1)
return false ;
if (L == R)
return true ;
if (K >= odd)
return true ;
else
return false ;
}
int main()
{
int L = 4;
int R = 10;
int K = 3;
bool isPossible = gcdOfArray(L, R, K);
if (isPossible)
cout << "true" << endl;
else
cout << "false" << endl;
return 0;
}
|
Java
import java.util.*;
class GFG
{
public static boolean gcdOfArray( int L, int R, int K)
{
int range = (R - L + 1 );
int even = 0 ;
int odd = 0 ;
if (range % 2 == 0 ) {
even = range / 2 ;
odd = range - even;
}
else {
if (L % 2 != 0 || R % 2 != 0 ) {
odd = (range / 2 ) + 1 ;
even = range - odd;
}
else {
odd = range / 2 ;
even = range - odd;
}
}
if (L == R && L == 1 )
return false ;
if (L == R)
return true ;
if (K >= odd)
return true ;
else
return false ;
}
public static void main(String[] args)
{
int L = 4 ;
int R = 10 ;
int K = 3 ;
boolean isPossible = gcdOfArray(L, R, K);
if (isPossible)
System.out.println( "true" );
else
System.out.println( "false" );
}
}
|
Python3
def gcdOfArray(L, R, K):
range = (R - L + 1 )
even = 0
odd = 0
if ( range % 2 = = 0 ):
even = range / / 2
odd = range - even
else :
if (L % 2 ! = 0 or R % 2 ! = 0 ):
odd = ( range / / 2 ) + 1
even = range - odd
else :
odd = range / / 2
even = range - odd
if (L = = R and L = = 1 ):
return False
if (L = = R):
return True
if (K > = odd):
return True
else :
return False
L = 4
R = 10
K = 3
isPossible = gcdOfArray(L, R, K)
if (isPossible):
print ( "true" )
else :
print ( "false" )
|
C#
using System;
public class GFG{
public static bool gcdOfArray( int L, int R, int K)
{
int range = (R - L + 1);
int even = 0;
int odd = 0;
if (range % 2 == 0) {
even = range / 2;
odd = range - even;
}
else {
if (L % 2 != 0 || R % 2 != 0) {
odd = (range / 2) + 1;
even = range - odd;
}
else {
odd = range / 2;
even = range - odd;
}
}
if (L == R && L == 1)
return false ;
if (L == R)
return true ;
if (K >= odd)
return true ;
else
return false ;
}
static public void Main (){
int L = 4;
int R = 10;
int K = 3;
bool isPossible = gcdOfArray(L, R, K);
if (isPossible)
Console.WriteLine( "true" );
else
Console.WriteLine( "false" );
}
}
|
Javascript
<script>
const gcdOfArray = (L, R, K) => {
let range = (R - L + 1);
let even = 0;
let odd = 0;
if (range % 2 == 0) {
even = parseInt(range / 2);
odd = range - even;
}
else {
if (L % 2 != 0 || R % 2 != 0) {
odd = parseInt(range / 2) + 1;
even = range - odd;
}
else {
odd = parseInt(range / 2);
even = range - odd;
}
}
if (L == R && L == 1)
return false ;
if (L == R)
return true ;
if (K >= odd)
return true ;
else
return false ;
}
let L = 4;
let R = 10;
let K = 3;
let isPossible = gcdOfArray(L, R, K);
if (isPossible)
document.write( "true" );
else
document.write( "false" );
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...