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++
// C++ program for the above approach
#include <bits/stdc++.h>
using namespace std;
// Function to find that GCD of array is
// greater than 1 or
// not after at most K operations
bool gcdOfArray(int L, int R, int K)
{
// Finding number of integers
// between L and R
int range = (R - L + 1);
int even = 0;
int odd = 0;
// Finding number of odd and even integers
// in the given range
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;
}
}
// Case 1
if (L == R && L == 1)
return false;
// Case 2
if (L == R)
return true;
// Case 3
if (K >= odd)
return true;
// Otherwise not possible
else
return false;
}
// Driver Code
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
// JAVA program for the above approach
import java.util.*;
class GFG
{
// Function to find that GCD of array is
// greater than 1 or
// not after at most K operations
public static boolean gcdOfArray(int L, int R, int K)
{
// Finding number of integers
// between L and R
int range = (R - L + 1);
int even = 0;
int odd = 0;
// Finding number of odd and even integers
// in the given range
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;
}
}
// Case 1
if (L == R && L == 1)
return false;
// Case 2
if (L == R)
return true;
// Case 3
if (K >= odd)
return true;
// Otherwise not possible
else
return false;
}
// Driver Code
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");
}
}
// This code is contributed by Taranpreet
Python3
# Python program for the above approach
# Function to find that GCD of array is
# greater than 1 or
# not after at most K operations
def gcdOfArray(L, R, K):
# Finding number of integers
# between L and R
range = (R - L + 1)
even = 0
odd = 0
# Finding number of odd and even integers
# in the given range
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
# Case 1
if (L == R and L == 1):
return False
# Case 2
if (L == R):
return True
# Case 3
if (K >= odd):
return True
# Otherwise not possible
else:
return False
# Driver Code
L = 4
R = 10
K = 3
isPossible = gcdOfArray(L, R, K)
if (isPossible):
print("true")
else:
print("false")
# This code is contributed by gfgking
C#
// C# program for the above approach
using System;
public class GFG{
// Function to find that GCD of array is
// greater than 1 or
// not after at most K operations
public static bool gcdOfArray(int L, int R, int K)
{
// Finding number of integers
// between L and R
int range = (R - L + 1);
int even = 0;
int odd = 0;
// Finding number of odd and even integers
// in the given range
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;
}
}
// Case 1
if (L == R && L == 1)
return false;
// Case 2
if (L == R)
return true;
// Case 3
if (K >= odd)
return true;
// Otherwise not possible
else
return false;
}
// Driver Code
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");
}
}
// This code is contributed by Shubham Singh
JavaScript
<script>
// JavaScript program for the above approach
// Function to find that GCD of array is
// greater than 1 or
// not after at most K operations
const gcdOfArray = (L, R, K) => {
// Finding number of integers
// between L and R
let range = (R - L + 1);
let even = 0;
let odd = 0;
// Finding number of odd and even integers
// in the given range
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;
}
}
// Case 1
if (L == R && L == 1)
return false;
// Case 2
if (L == R)
return true;
// Case 3
if (K >= odd)
return true;
// Otherwise not possible
else
return false;
}
// Driver Code
let L = 4;
let R = 10;
let K = 3;
let isPossible = gcdOfArray(L, R, K);
if (isPossible)
document.write("true");
else
document.write("false");
// This code is contributed by rakeshsahni
</script>
Time Complexity: O(1)
Auxiliary Space: O(1)
Similar Reads
Check if GCD of [L, R] can be made >1 by replacing pairs with their product at most K times 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: YesEx
5 min read
Check if an Array can be converted to other by replacing pairs with GCD Given arrays A[] and B[] each of length N and A[i] has all the elements from 1 to N, the task is to check if it is possible to convert A[] to B[] by replacing any pair of A[] with their GCD. Examples: Input: N = 2, A[] = {1, 2}, B[] = {1, 1}Output: YES Explanation:First Operation - For A[], choose i
15 min read
Maximum count of pairs in Array with GCD greater than 1 by reordering given Array Given an array arr[] of size N. The task is to reorder arr[] and find the maximum number of GCD pairs which follows the conditions given below. Choose any two elements of array Ai and Aj of array where 0 <= i < j < N.Calculate GCD after multiplying Aj with 2 like (Ai, 2 * Aj) which is great
6 min read
Check if array can be sorted by swapping pairs with GCD of set bits count equal to that of the smallest array element Given an array arr[] consisting of N integers, the task is to check if it is possible to sort the array using the following swap operations: Swapping of two numbers is valid only if the Greatest Common Divisor of count of set bits of the two numbers is equal to the number of set bits in the smallest
9 min read
Modify sequence of first N natural numbers to a given array by replacing pairs with their GCD Given an integer N and an array arr[], the task is to check if a sequence of first N natural numbers, i.e. {1, 2, 3, .. N} can be made equal to arr[] by choosing any pair (i, j) from the sequence and replacing both i and j by GCD of i and j. If possible, then print "Yes". Otherwise, print "No". Exam
11 min read