Minimum increments required to make pair of X with Array elements non co-prime
Given an integer arr[] of length N such that (Ai > 0) for all (1 ? i ? N) and an integer X. In one operation you can add 1 to either X or arr[i] to make the GCD(X, Ai) not equal to 1. Remember that X or arr[i] increment in values will be permanent. Formally, If any value is incremented in any operation then the original one will also alter.
Then your task is to print all the below-mentioned things in the output:
- The minimum number of operations required to make GCD(Xi, arr[i]) is not equal to 1 for all (1 ? i ? N).
- updated arr[]
- values of X for each valid i.
Examples:
Input 1: N = 5, arr[] = {4,3,1,4,6}, X = 3
Output 1: 3
arr[] = {4, 4, 2, 4, 6}
X[] = {4, 4, 4, 4, 4}
Explanation: There are minimum 3 operations required so that GCD(Xi, arr[i]) ? 1. Three operations are defined below:
- At index 1: Initially arr[1] = 4 and X = 3. GCD(3, 4) = 1.Therefore, X increment to 3+1=4. So that GCD(4, 4) =4, Which is not equal to 1 now. X altered from 3 to 4 permanently for next values of index i.
- At index 2: arr[2] = 3 and X = 4. GCD(3, 4) = 1.Therefore, arr[2] increment to 3+1 = 4. So that GCD(4, 4) = 4, Which is not equal to 1. arr[2] altered from 3 to 4 permanently.
- At index 3: arr[3] altered to 1+1 = 2 and X = 4. So that GCD(X, arr[3]) at index 3 is : GCD(4, 2) = 2, Which is not equal to 1.
It can be verified that there is no such pair of Xi and arr[i] exist for (1<=i<=N) in output such that GCD(Xi, arr[i]) =1. So, Minimum operations required are 3.
Input 2: N = 3, arr[] = {4, 8, 2}, X = 2
Output 2: 0
arr[] = {4, 8, 2}
X[] = {2, 2, 2}
Explanation: All the pair of X and arr[i] for each valid i has GCD not equal to 1.Therefore, 0 operations required.
Approach: Implement the idea below to solve the problem:
GCD of any two integers let say X and Y can be made greater than 1 easily under 0, 1 or 2 operations. By following this hint we can alter the values of X or arr[i] to make GCD greater than 1. For clear explanation see the concept of approach below.
Concept of approach:
For this problem we just need to find to minimum operation required to make GCD of two number greater than 1 at each valid position of i, Then altering the value of X or arr[i] is not a big deal. The problem can be divided in sub-parts as :
1. if X and arr[i] both are even at any index i or the GCD(X, arr[i]) is already greater than 1, Then:
- X and arr[i] will remain same.
- Minimum Operations required = 0.
2. if value of either GCD(arr[i]+1, X) or GCD(arr[i], X+1) is not equal to 1, Then:
- Increment the element X or arr[i], Which gives GCD greater than 1.
- Minimum Operations required = 1.
3. If none of the above condition met, Then:
- Increment both X and arr[i].
- Minimum Operations required= 2 .
It can be verified that the value of GCD(Xi , arr[i]) can make greater than 1 by one of the above operation all the possible pairs of X and arr[i].
Illustration of approach(By using above discussed 3 sub-parts of the problem):
Input: N = 4, arr[] = {1, 4, 3, 5}, X = 1
Output: Minimum operations required : 4
arr[]: [2, 4, 4, 6]
Values of X: [2, 2, 2, 2]
Explanation:
At index 1: arr[1] = 1, X =1, GCD(1, 1) = 1
- GCD can be altered from 1 using sub-part 3 of the problem, Which is incrementing both X and arr[i]. Then,
- GCD = (X+1, arr[1]+1) = GCD(2, 2) = 2 ? 1. X and arr[1] altered to 2 and 2 respectively. Minimum operations required = 2. Total operations till now = 2.
At index 2: arr[2] = 4, X = 2, GCD(4, 2) = 2,
- Already not equal to 1 and related to sub-part 1 of the problem, Which is not to alter any of X or arr[i]. Minimum operations required = 0. Total operations till now = 2.
At index 3: arr[3] = 3, X = 2, GCD(3, 2) = 1
- GCD can be altered using sub-part 2 of the problem, Which is incrementing arr[i]. Then,
- GCD = (X, arr[1]+1) = GCD(4, 2) = 2 ? 1. X and arr[3] altered to 2 and 4 respectively. Minimum operations required = 1. Till now total operations = 3.
At index 4: arr[4] = 5, X = 2, GCD(5, 2) = 1
- GCD can be altered using sub-part 2 of the problem, Which is incrementing arr[i]. Then,
- GCD = (X, arr[1]+1) = GCD(2, 5+1) = 2 ? 1. X and arr[4] altered to 2 and 6 respectively. Minimum operations required = 1. Total operations till now = 4.
So, Total number of operations required are 4. Altered values of X or arr[i] will permanent.
arr[] = {arr[1], arr[2], arr[3], arr[4]} = {2, 4, 4, 6} (updated arr[] with altered values)
X[] = {2, 2, 2, 2}
Below is the implementation for the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int GCD( int a, int b)
{
return b == 0 ? a : GCD(b, a % b);
}
int main()
{
int n = 5;
int arr[] = {4, 3, 1, 4, 6};
int X = 3;
int min_operation = 0;
vector< int > X_Values;
for ( int i = 0; i < n; i++)
{
int gcd = GCD(X, arr[i]);
if (X % 2 == 0 && arr[i] % 2 == 0 || gcd > 1)
{
min_operation += 0;
X_Values.push_back(X);
}
else if (arr[i] % 2 == 0 || X % 2 == 0)
{
if (arr[i] % 2 == 0)
{
X_Values.push_back(X + 1);
X += 1;
}
else
{
arr[i] += 1;
X_Values.push_back(X);
}
min_operation += 1;
}
else
{
if (GCD(arr[i] + 1, X) > 1 || GCD(arr[i], X + 1) > 1)
{
if (GCD(arr[i] + 1, X) > 1)
{
arr[i] += 1;
X_Values.push_back(X);
}
else
{
X_Values.push_back(X + 1);
X += 1;
}
min_operation += 1;
}
else
{
arr[i] += 1;
X_Values.push_back(X + 1);
X += 1;
min_operation += 2;
}
}
}
cout << "Minimum operations required : "
<< min_operation;
cout << endl;
cout << "arr[] : " ;
for ( int i = 0; i < n; i++)
cout << arr[i] << " " ;
cout << endl;
cout << "Values of X : " ;
for ( int i = 0; i < X_Values.size(); i++)
{
cout << X_Values[i] << " " ;
}
}
|
Java
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
public static void main(String args[])
{
int n = 5 ;
int arr[] = { 4 , 3 , 1 , 4 , 6 };
int X = 3 ;
int min_operation = 0 ;
ArrayList<Integer> X_Values = new ArrayList<>();
for ( int i = 0 ; i < n; i++) {
int gcd = GCD(X, arr[i]);
if (X % 2 == 0 && arr[i] % 2 == 0 || gcd > 1 ) {
min_operation += 0 ;
X_Values.add(X);
}
else if (arr[i] % 2 == 0 || X % 2 == 0 ) {
if (arr[i] % 2 == 0 ) {
X_Values.add(X + 1 );
X += 1 ;
}
else {
arr[i] += 1 ;
X_Values.add(X);
}
min_operation += 1 ;
}
else {
if (GCD(arr[i] + 1 , X) > 1
|| GCD(arr[i], X + 1 ) > 1 ) {
if (GCD(arr[i] + 1 , X) > 1 ) {
arr[i] += 1 ;
X_Values.add(X);
}
else {
X_Values.add(X + 1 );
X += 1 ;
}
min_operation += 1 ;
}
else {
arr[i] += 1 ;
X_Values.add(X + 1 );
X += 1 ;
min_operation += 2 ;
}
}
}
System.out.println( "Minimum operations required : "
+ min_operation);
System.out.println( "arr[] : "
+ Arrays.toString(arr));
System.out.println( "Values of X : " + X_Values);
}
static int GCD( int a, int b)
{
return b == 0 ? a : GCD(b, a % b);
}
}
|
Python3
def GCD(a, b) :
if b = = 0 :
return a
else :
return GCD(b, a % b)
if __name__ = = "__main__" :
n = 5 ;
arr = [ 4 , 3 , 1 , 4 , 6 ];
X = 3 ;
min_operation = 0 ;
X_Values = [];
for i in range (n) :
gcd = GCD(X, arr[i]);
if (X % 2 = = 0 and arr[i] % 2 = = 0 or gcd > 1 ) :
min_operation + = 0 ;
X_Values.append(X);
elif (arr[i] % 2 = = 0 or X % 2 = = 0 ) :
if (arr[i] % 2 = = 0 ) :
X_Values.append(X + 1 );
X + = 1 ;
else :
arr[i] + = 1 ;
X_Values.append(X);
min_operation + = 1 ;
else :
if (GCD(arr[i] + 1 , X) > 1 or GCD(arr[i], X + 1 ) > 1 ) :
if (GCD(arr[i] + 1 , X) > 1 ) :
arr[i] + = 1 ;
X_Values.append(X);
else :
X_Values.append(X + 1 );
X + = 1 ;
min_operation + = 1 ;
else :
arr[i] + = 1 ;
X_Values.append(X + 1 );
X + = 1 ;
min_operation + = 2 ;
print ( "Minimum operations required : " ,min_operation)
print ( "arr[] : " ,end = "");
for i in range (n) :
print (arr[i],end = " " );
print ( "\n Values of X : " ,end = "");
for i in range ( len (X_Values)) :
print (X_Values[i],end = " " );
|
C#
using System;
public class GFG {
static int GCD( int a, int b)
{
return b == 0 ? a : GCD(b, a % b);
}
static public void Main()
{
int n = 5;
int [] arr = { 4, 3, 1, 4, 6 };
int X = 3;
int [] X_Values= new int [5];
int min_operation = 0;
for ( int i = 0; i < n; i++)
{
int gcd = GCD(X, arr[i]);
if (X % 2 == 0 && arr[i] % 2 == 0 || gcd > 1) {
min_operation += 0;
X_Values[i] = X;
}
else if (arr[i] % 2 == 0 || X % 2 == 0) {
if (arr[i] % 2 == 0)
{
X_Values[i] = (X + 1);
X += 1;
}
else {
arr[i] += 1;
X_Values[i] = (X);
}
min_operation += 1;
}
else {
if (GCD(arr[i] + 1, X) > 1
|| GCD(arr[i], X + 1) > 1) {
if (GCD(arr[i] + 1, X) > 1) {
arr[i] += 1;
X_Values[i] = (X);
}
else {
X_Values[i] = (X + 1);
X += 1;
}
min_operation += 1;
}
else {
arr[i] += 1;
X_Values[i] = (X + 1);
X += 1;
min_operation += 2;
}
}
}
Console.Write( "Minimum operations required : " +
min_operation);
Console.WriteLine();
Console.Write( "arr[] : " );
for ( int i = 0; i < n; i++)
Console.Write(arr[i] + " " );
Console.WriteLine();
Console.Write( "Values of X : " );
for ( int i = 0; i < X_Values.Length; i++) {
Console.Write(X_Values[i] + " " );
}
}
}
|
Javascript
function GCD(a,b)
{
return b == 0 ? a : GCD(b, a % b);
}
let n = 5;
let arr = [4, 3, 1, 4, 6];
let X = 3;
let min_operation = 0;
let X_Values=[];
for (let i = 0; i < n; i++)
{
let gcd = GCD(X, arr[i]);
if (X % 2 == 0 && arr[i] % 2 == 0 || gcd > 1)
{
min_operation += 0;
X_Values[i]=(X);
}
else if (arr[i] % 2 == 0 || X % 2 == 0)
{
if (arr[i] % 2 == 0)
{
X_Values[i]=(X + 1);
X += 1;
}
else
{
arr[i] += 1;
X_Values[i]=(X);
}
min_operation += 1;
}
else
{
if (GCD(arr[i] + 1, X) > 1 || GCD(arr[i], X + 1) > 1)
{
if (GCD(arr[i] + 1, X) > 1)
{
arr[i] += 1;
X_Values[i]=(X);
}
else
{
X_Values[i]=(X + 1);
X += 1;
}
min_operation += 1;
}
else
{
arr[i] += 1;
X_Values[i]=(X + 1);
X += 1;
min_operation += 2;
}
}
}
console.log( "Minimum operations required :" ,min_operation);
console.log( "arr[] : " );
for (let i = 0; i < n; i++){
console.log(arr[i]);
}
console.log( "Values of X : " );
for (let i = 0; i < X_Values.length; i++)
{
console.log(X_Values[i]);
}
|
Output
Minimum operations required : 3
arr[] : [4, 4, 2, 4, 6]
Values of X : [4, 4, 4, 4, 4]
Time Complexity: O(N*log(N))
Auxiliary Space: O(N)
Last Updated :
17 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...