Convert the array such that the GCD of the array becomes 1
Last Updated :
19 Aug, 2022
Given an array of positive elements and a positive integer k, the task is to convert the GCD of the array to 1. To make it possible, only one operation is allowed any number of times i.e. choose any element of the array & divide with a number d where d <= k.
Examples:
Input : arr = {10, 15, 30}, k = 6
Output : Yes
Divide all the elements of array by 5 as it is the divisor of all elements and also less than k i.e. 6. It gives as the sequence like {2, 3, 6}. Now, divide 2 by 2, 3 by 3 and 6 by 2. Then, the sequence becomes {1, 1, 3}. Now, the gcd of the array is 1.
Input : arr = {5, 10, 20}, k = 4
Output : No
Here, divide 10 with 2 the sequence becomes {5, 5, 20}. Then, divide 20 by 2 and again by 2. Finally, the sequence becomes {5, 5, 5}. To make the gcd of this sequence 1, divide each element by 5. But 5 > 4 so here it is impossible to make the gcd 1.
Approach:
- If there exists a positive prime number greater than k that divides each element of the array then the answer is No.
- If the largest prime factor of the GCD of the array is less than or equal to k then the answer is Yes.
- First, find the GCD of the array then check if there exists a prime factor of the GCD that is greater than k.
- For this, calculate largest prime factor of GCD.
Implementation:
C++
// C++ program to check if it is
// possible to convert the gcd of
// the array to 1 by applying the
// given operation
#include <bits/stdc++.h>
using namespace std;
// Function to get gcd of the array.
int getGcd(int* arr, int n)
{
int gcd = arr[0];
for (int i = 1; i < n; i++)
gcd = __gcd(arr[i], gcd);
return gcd;
}
// Function to check if it is possible.
bool convertGcd(int* arr, int n, int k)
{
// Getting the gcd of array
int gcd = getGcd(arr, n);
// Initially taking max_prime factor is 1.
int max_prime = 1;
// find maximum of all the prime factors
// till sqrt(gcd).
for (int i = 2; i <= sqrt(gcd); i++) {
while (gcd % i == 0) {
gcd /= i;
max_prime = max(max_prime, i);
}
}
// either GCD is reduced to 1 or a prime factor
// greater than sqrt(gcd)
max_prime = max(max_prime, gcd);
return (max_prime <= k);
}
// Drivers code
int main()
{
int arr[] = { 10, 15, 30 };
int k = 6;
int n = sizeof(arr) / sizeof(arr[0]);
if (convertGcd(arr, n, k) == true)
cout << "Yes";
else
cout << "No";
return 0;
}
Java
// Java program to check if it is
// possible to convert the gcd of
// the array to 1 by applying the
// given operation
import java.io.*;
class GFG {
// Recursive function to return
// gcd of a and b
static int __gcd(int a, int b)
{
// Everything divides 0
if (a == 0 || b == 0)
return 0;
// base case
if (a == b)
return a;
// a is greater
if (a > b)
return __gcd(a-b, b);
return __gcd(a, b-a);
}
// Function to get gcd of the array.
static int getGcd(int arr[], int n)
{
int gcd = arr[0];
for (int i = 1; i < n; i++)
gcd = __gcd(arr[i], gcd);
return gcd;
}
// Function to check if it is possible.
static boolean convertGcd(int []arr,
int n, int k)
{
// Getting the gcd of array
int gcd = getGcd(arr, n);
// Initially taking max_prime
// factor is 1.
int max_prime = 1;
// find maximum of all the prime
// factors till sqrt(gcd).
for (int i = 2; i <= Math.sqrt(gcd);
i++)
{
while (gcd % i == 0) {
gcd /= i;
max_prime =
Math.max(max_prime, i);
}
}
// either GCD is reduced to 1 or a
// prime factor greater than sqrt(gcd)
max_prime = Math.max(max_prime, gcd);
return (max_prime <= k);
}
// Drivers code
public static void main (String[] args)
{
int []arr = { 10, 15, 30 };
int k = 6;
int n = arr.length;
if (convertGcd(arr, n, k) == true)
System.out.println( "Yes");
else
System.out.println( "No");
}
}
// This code is contributed by anuj_67.
Python3
# Python 3 program to check if it is
# possible to convert the gcd of
# the array to 1 by applying the
# given operation
from math import gcd as __gcd, sqrt
# Function to get gcd of the array.
def getGcd(arr, n):
gcd = arr[0];
for i in range(1, n, 1):
gcd = __gcd(arr[i], gcd)
return gcd
# Function to check if it is possible.
def convertGcd(arr, n, k):
# Getting the gcd of array
gcd = getGcd(arr, n)
# Initially taking max_prime
# factor is 1.
max_prime = 1
# find maximum of all the
# prime factors till sqrt(gcd)
p = int(sqrt(gcd)) + 1
for i in range(2, p, 1):
while (gcd % i == 0):
gcd = int(gcd / i)
max_prime = max(max_prime, i)
# either GCD is reduced to 1 or a
# prime factor greater than sqrt(gcd)
max_prime = max(max_prime, gcd)
return (max_prime <= k)
# Drivers code
if __name__ == '__main__':
arr = [10, 15, 30]
k = 6
n = len(arr)
if (convertGcd(arr, n, k) == True):
print("Yes")
else:
print("No")
# This code is contributed by
# Sahil_Shelangia
C#
// C# program to check if it is
// possible to convert the gcd of
// the array to 1 by applying the
// given operation
using System;
class GFG {
// Recursive function to return
// gcd of a and b
static int __gcd(int a, int b)
{
// Everything divides 0
if (a == 0 || b == 0)
return 0;
// base case
if (a == b)
return a;
// a is greater
if (a > b)
return __gcd(a-b, b);
return __gcd(a, b-a);
}
// Function to get gcd of the array.
static int getGcd(int []arr, int n)
{
int gcd = arr[0];
for (int i = 1; i < n; i++)
gcd = __gcd(arr[i], gcd);
return gcd;
}
// Function to check if it is possible.
static bool convertGcd(int []arr,
int n, int k)
{
// Getting the gcd of array
int gcd = getGcd(arr, n);
// Initially taking max_prime
// factor is 1.
int max_prime = 1;
// find maximum of all the prime
// factors till sqrt(gcd).
for (int i = 2; i <= Math.Sqrt(gcd);
i++)
{
while (gcd % i == 0) {
gcd /= i;
max_prime =
Math.Max(max_prime, i);
}
}
// either GCD is reduced to 1 or a
// prime factor greater than sqrt(gcd)
max_prime = Math.Max(max_prime, gcd);
return (max_prime <= k);
}
// Drivers code
public static void Main ()
{
int []arr = { 10, 15, 30 };
int k = 6;
int n = arr.Length;
if (convertGcd(arr, n, k) == true)
Console.WriteLine( "Yes");
else
Console.WriteLine( "No");
}
}
// This code is contributed by anuj_67.
PHP
<?php
// PHP program to check if it is
// possible to convert the gcd of
// the array to 1 by applying the
// given operation
// Recursive function to
// return gcd of a and b
function __gcd($a, $b)
{
// Everything divides 0
if($a == 0 or $b == 0)
return 0 ;
// base case
if($a == $b)
return $a ;
// a is greater
if($a > $b)
return __gcd($a - $b , $b) ;
return __gcd( $a , $b - $a) ;
}
// Function to get gcd of the array.
function getGcd($arr, $n)
{
$gcd = $arr[0];
for ($i = 1; $i < $n; $i++)
$gcd = __gcd($arr[$i], $gcd);
return $gcd;
}
// Function to check if it is possible.
function convertGcd( $arr, $n, $k)
{
// Getting the gcd of array
$gcd = getGcd($arr, $n);
// Initially taking max_prime
// factor is 1.
$max_prime = 1;
// find maximum of all the prime
// factors till sqrt(gcd).
for($i = 2; $i <= sqrt($gcd); $i++)
{
while ($gcd % $i == 0)
{
$gcd /= $i;
$max_prime = max($max_prime, $i);
}
}
// either GCD is reduced
// to 1 or a prime factor
// greater than sqrt(gcd)
$max_prime = max($max_prime, $gcd);
return ($max_prime <= $k);
}
// Driver Code
$arr = array(10, 15, 30);
$k = 6;
$n = count($arr);
if (convertGcd($arr, $n, $k) == true)
echo "Yes";
else
echo "No";
// This code is contributed by anuj_67.
?>
JavaScript
<script>
// Javascript program to check if it is
// possible to convert the gcd of
// the array to 1 by applying the
// given operation
// Recursive function to return
// gcd of a and b
function __gcd(a, b)
{
// Everything divides 0
if (a == 0 || b == 0)
return 0;
// base case
if (a == b)
return a;
// a is greater
if (a > b)
return __gcd(a-b, b);
return __gcd(a, b-a);
}
// Function to get gcd of the array.
function getGcd(arr, n)
{
let gcd = arr[0];
for (let i = 1; i < n; i++)
gcd = __gcd(arr[i], gcd);
return gcd;
}
// Function to check if it is possible.
function convertGcd(arr, n, k)
{
// Getting the gcd of array
let gcd = getGcd(arr, n);
// Initially taking max_prime
// factor is 1.
let max_prime = 1;
// find maximum of all the prime
// factors till sqrt(gcd).
for (let i = 2; i <= Math.sqrt(gcd); i++)
{
while (gcd % i == 0) {
gcd = parseInt(gcd / i, 10);
max_prime = Math.max(max_prime, i);
}
}
// either GCD is reduced to 1 or a
// prime factor greater than sqrt(gcd)
max_prime = Math.max(max_prime, gcd);
return (max_prime <= k);
}
let arr = [ 10, 15, 30 ];
let k = 6;
let n = arr.length;
if (convertGcd(arr, n, k) == true)
document.write("Yes");
else
document.write( "No");
// This code is contributed by divyesh072019.
</script>
Complexity Analysis:
- Time Complexity: O(n), to iterate over the array
- Auxiliary Space: O(1), as no extra space is required
Similar Reads
Array with GCD of any of its subset belongs to the given array Given a set of N elements such that N\in [1, 1000] , task is to generate an array such that the GCD of any subset of the generated array lies in the given set of elements. The generated array should not be more than thrice the length of the set of the GCD. Prerequisite : GCD of an Array | Subset of
8 min read
Modify array such that the array does not contain any common divisors other than 1 Given an array arr[] consisting of N integers and an integer X, the task is to check if it is possible to modify the array such that the array does not contain any common divisor other than 1, by repeatedly dividing any array element of the array by any of its divisor d (d ⤠X). If it is possible to
7 min read
Construct an Array such that GCD of each element with their indices is unique Given two integers L and R, construct an array of given size N such that the GCD (Greatest Common Divisor) of the element with their indices (1-based indexing) is unique for every index. The task is to print the array or output -1 if it is impossible to do so. Examples: Input: N = 10, L = 1, R = 15O
7 min read
Count of elements altering which changes the GCD of Array Given an array arr[] of size N, the task is to count the number of indices in arr[] such that after changing the element of that index to any number, the GCD of the array is changed. Examples: Input: arr[] = {3, 6, 9}Output: 3Explanation: The GCD of the array is 3.If we change 3 to 4, the GCD of arr
9 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