Minimum increments required to make pair of X with Array elements non co-prime
Last Updated :
17 Feb, 2023
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++
// C++ code to implement the approach
#include <bits/stdc++.h>
using namespace std;
// Euclidean algorithm to return GCD
// of 2 input integers
int GCD(int a, int b)
{
return b == 0 ? a : GCD(b, a % b);
}
// Driver Function
int main()
{
// Input value of N
int n = 5;
// Input arr[]
int arr[] = {4, 3, 1, 4, 6};
// Input value of X
int X = 3;
// variable to store minimum
// number of operations required
int min_operation = 0;
// ArrayList to store altered or
// non altered value of X at
// each index
vector<int> X_Values;
// loop for traversing on input arr[]
for (int i = 0; i < n; i++)
{
// Calculating gcd of X and
// arr[i] by user defined
// GCD function
int gcd = GCD(X, arr[i]);
// If both X and arr[i] are
// even or already GCD > 1
if (X % 2 == 0 && arr[i] % 2 == 0 || gcd > 1)
{
// Required operations for
// this type of case = 0
min_operation += 0;
// Adding current value
// of X to list
X_Values.push_back(X);
}
// If any of the X or arr[i]
// is even
else if (arr[i] % 2 == 0 || X % 2 == 0)
{
// If arr[i] is even
if (arr[i] % 2 == 0)
{
// Add incremented
// value of X to list
X_Values.push_back(X + 1);
// Increment value of
// X permanently
X += 1;
}
else
{
// Incremented value
// of arr[i]
arr[i] += 1;
// Adding current
// value of X to list
X_Values.push_back(X);
}
// Increase the value of
// min_operations variable
min_operation += 1;
}
else
{
// If incrementing one of
// the arr[i] or X gives GCD>1
if (GCD(arr[i] + 1, X) > 1 || GCD(arr[i], X + 1) > 1)
{
// If incrementing arr[i]
// gives GCD>1
if (GCD(arr[i] + 1, X) > 1)
{
// Increment arr[i]
arr[i] += 1;
// Add current Value
// of X to list
X_Values.push_back(X);
}
// If incrementing X gives GCD>1 the
// else part will execute
else
{
// Adding incremented value of X to
// list
X_Values.push_back(X + 1);
// altered value of X permanently
X += 1;
}
// Increment min_operation variable
min_operation += 1;
}
// if incrementing both arr[i] and X gives
// GCD>, Then this else part will execute
else
{
// Incrementing both arr[i] and X
arr[i] += 1;
X_Values.push_back(X + 1);
// Altering X value permanently
X += 1;
// Operation required for this type case
// is 2.Because in first operation arr[i]
// increased and X in second operation.
min_operation += 2;
}
}
}
// Printing value of minimum number
// of operations required
cout << "Minimum operations required : "
<< min_operation;
cout << endl;
// Printing updated arr[]
cout << "arr[] : ";
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
cout << endl;
// Printing values of X for each index
cout << "Values of X : ";
for (int i = 0; i < X_Values.size(); i++)
{
cout << X_Values[i] << " ";
}
}
// This code is contributed by Potta Lokesh
Java
// Java code to implement the approach
import java.io.*;
import java.lang.*;
import java.util.*;
class GFG {
// Driver Function
public static void main(String args[])
{
// Input value of N
int n = 5;
// Input arr[]
int arr[] = { 4, 3, 1, 4, 6 };
// Input value of X
int X = 3;
// variable to store minimum
// number of operations required
int min_operation = 0;
// ArrayList to store altered or
// non altered value of X at
// each index
ArrayList<Integer> X_Values = new ArrayList<>();
// loop for traversing on input arr[]
for (int i = 0; i < n; i++) {
// Calculating gcd of X and
// arr[i] by user defined
// GCD function
int gcd = GCD(X, arr[i]);
// If both X and arr[i] are
// even or already GCD > 1
if (X % 2 == 0 && arr[i] % 2 == 0 || gcd > 1) {
// Required operations for
// this type of case = 0
min_operation += 0;
// Adding current value
// of X to list
X_Values.add(X);
}
// If any of the X or arr[i]
// is even
else if (arr[i] % 2 == 0 || X % 2 == 0) {
// If arr[i] is even
if (arr[i] % 2 == 0) {
// Add incremented
// value of X to list
X_Values.add(X + 1);
// Increment value of
// X permanently
X += 1;
}
else {
// Incremented value
// of arr[i]
arr[i] += 1;
// Adding current
// value of X to list
X_Values.add(X);
}
// Increase the value of
// min_operations variable
min_operation += 1;
}
else {
// If incrementing one of
// the arr[i] or X gives GCD>1
if (GCD(arr[i] + 1, X) > 1
|| GCD(arr[i], X + 1) > 1) {
// If incrementing arr[i]
// gives GCD>1
if (GCD(arr[i] + 1, X) > 1) {
// Increment arr[i]
arr[i] += 1;
// Add current Value
// of X to list
X_Values.add(X);
}
// If incrementing X gives GCD>1 the
// else part will execute
else {
// Adding incremented value of X to
// list
X_Values.add(X + 1);
// altered value of X permanently
X += 1;
}
// Increment min_operation variable
min_operation += 1;
}
// if incrementing both arr[i] and X gives
// GCD>, Then this else part will execute
else {
// Incrementing both arr[i] and X
arr[i] += 1;
X_Values.add(X + 1);
// Altering X value permanently
X += 1;
// Operation required for this type case
// is 2.Because in first operation arr[i]
// increased and X in second operation.
min_operation += 2;
}
}
}
// Printing value of minimum number
// of operations required
System.out.println("Minimum operations required : "
+ min_operation);
// Printing updated arr[]
System.out.println("arr[] : "
+ Arrays.toString(arr));
// Printing values of X for each index
System.out.println("Values of X : " + X_Values);
}
// Euclidean algorithm to return GCD
// of 2 input integers
static int GCD(int a, int b)
{
return b == 0 ? a : GCD(b, a % b);
}
}
Python3
# Python3 code to implement the above approach
# Euclidean algorithm to return GCD
# of 2 input integers
def GCD(a, b) :
if b == 0 :
return a
else :
return GCD(b, a % b)
# Driver Function
if __name__ == "__main__" :
# Input value of N
n = 5;
# Input arr[]
arr = [4, 3, 1, 4, 6];
# Input value of X
X = 3;
# variable to store minimum
# number of operations required
min_operation = 0;
# ArrayList to store altered or
# non altered value of X at
# each index
X_Values = [];
# loop for traversing on input arr[]
for i in range(n) :
# Calculating gcd of X and
# arr[i] by user defined
# GCD function
gcd = GCD(X, arr[i]);
# If both X and arr[i] are
# even or already GCD > 1
if (X % 2 == 0 and arr[i] % 2 == 0 or gcd > 1) :
# Required operations for
# this type of case = 0
min_operation += 0;
# Adding current value
# of X to list
X_Values.append(X);
# If any of the X or arr[i]
# is even
elif (arr[i] % 2 == 0 or X % 2 == 0) :
# If arr[i] is even
if (arr[i] % 2 == 0) :
# Add incremented
# value of X to list
X_Values.append(X + 1);
# Increment value of
# X permanently
X += 1;
else :
# Incremented value
# of arr[i]
arr[i] += 1;
# Adding current
# value of X to list
X_Values.append(X);
# Increase the value of
# min_operations variable
min_operation += 1;
else :
# If incrementing one of
# the arr[i] or X gives GCD>1
if (GCD(arr[i] + 1, X) > 1 or GCD(arr[i], X + 1) > 1) :
# If incrementing arr[i]
# gives GCD>1
if (GCD(arr[i] + 1, X) > 1) :
# Increment arr[i]
arr[i] += 1;
# Add current Value
# of X to list
X_Values.append(X);
# If incrementing X gives GCD>1 the
# else part will execute
else :
# Adding incremented value of X to
# list
X_Values.append(X + 1);
# altered value of X permanently
X += 1;
# Increment min_operation variable
min_operation += 1;
# if incrementing both arr[i] and X gives
# GCD>, Then this else part will execute
else :
# Incrementing both arr[i] and X
arr[i] += 1;
X_Values.append(X + 1);
# Altering X value permanently
X += 1;
# Operation required for this type case
# is 2.Because in first operation arr[i]
# increased and X in second operation.
min_operation += 2;
# Printing value of minimum number
# of operations required
print("Minimum operations required : ",min_operation)
# Printing updated arr[]
print("arr[] : ",end="");
for i in range(n) :
print(arr[i],end=" ");
# Printing values of X for each index
print("\n Values of X : ",end="");
for i in range(len(X_Values)) :
print(X_Values[i],end= " ");
# This code is contributed by AnkThon
C#
// C# implementation
using System;
public class GFG {
// Euclidean algorithm to return GCD
// of 2 input integers
static int GCD(int a, int b)
{
return b == 0 ? a : GCD(b, a % b);
}
static public void Main()
{
// Input value of N
int n = 5;
// Input arr
int[] arr = { 4, 3, 1, 4, 6 };
// Input value of X
int X = 3;
int[] X_Values=new int[5];
// variable to store minimum
// number of operations required
int min_operation = 0;
// ArrayList to store altered or
// non altered value of X at
// each index
// loop for traversing on input arr[]
for (int i = 0; i < n; i++)
{
// Calculating gcd of X and
// arr[i] by user defined
// GCD function
int gcd = GCD(X, arr[i]);
// If both X and arr[i] are
// even or already GCD > 1
if (X % 2 == 0 && arr[i] % 2 == 0 || gcd > 1) {
// Required operations for
// this type of case = 0
min_operation += 0;
// Adding current value
// of X to list
X_Values[i] = X;
}
// If any of the X or arr[i]
// is even
else if (arr[i] % 2 == 0 || X % 2 == 0) {
// If arr[i] is even
if (arr[i] % 2 == 0)
{
// Add incremented
// value of X to list
X_Values[i] = (X + 1);
// Increment value of
// X permanently
X += 1;
}
else {
// Incremented value
// of arr[i]
arr[i] += 1;
// Adding current
// value of X to list
X_Values[i] = (X);
}
// Increase the value of
// min_operations variable
min_operation += 1;
}
else {
// If incrementing one of
// the arr[i] or X gives GCD>1
if (GCD(arr[i] + 1, X) > 1
|| GCD(arr[i], X + 1) > 1) {
// If incrementing arr[i]
// gives GCD>1
if (GCD(arr[i] + 1, X) > 1) {
// Increment arr[i]
arr[i] += 1;
// Add current Value
// of X to list
X_Values[i] = (X);
}
// If incrementing X gives GCD>1 the
// else part will execute
else {
// Adding incremented value of X to
// list
X_Values[i] = (X + 1);
// altered value of X permanently
X += 1;
}
// Increment min_operation variable
min_operation += 1;
}
// if incrementing both arr[i] and X gives
// GCD>, Then this else part will execute
else {
// Incrementing both arr[i] and X
arr[i] += 1;
X_Values[i] = (X + 1);
// Altering X value permanently
X += 1;
// Operation required for this type case
// is 2.Because in first operation
// arr[i] increased and X in second
// operation.
min_operation += 2;
}
}
}
// Printing value of minimum number
// of operations required
Console.Write("Minimum operations required : "+
min_operation);
Console.WriteLine();
// Printing updated arr[]
Console.Write("arr[] : ");
for (int i = 0; i < n; i++)
Console.Write(arr[i] + " ");
Console.WriteLine();
// Printing values of X for each index
Console.Write("Values of X : ");
for (int i = 0; i < X_Values.Length; i++) {
Console.Write(X_Values[i] + " ");
}
}
}
// This code is contributed by ksam24000
JavaScript
// JS code to implement the approach
// Euclidean algorithm to return GCD
// of 2 input integers
function GCD(a,b)
{
return b == 0 ? a : GCD(b, a % b);
}
// Driver Function
// Input value of N
let n = 5;
// Input arr[]
let arr = [4, 3, 1, 4, 6];
// Input value of X
let X = 3;
// variable to store minimum
// number of operations required
let min_operation = 0;
// ArrayList to store altered or
// non altered value of X at
// each index
let X_Values=[];
// loop for traversing on input arr[]
for (let i = 0; i < n; i++)
{
// Calculating gcd of X and
// arr[i] by user defined
// GCD function
let gcd = GCD(X, arr[i]);
// If both X and arr[i] are
// even or already GCD > 1
if (X % 2 == 0 && arr[i] % 2 == 0 || gcd > 1)
{
// Required operations for
// this type of case = 0
min_operation += 0;
// Adding current value
// of X to list
X_Values[i]=(X);
}
// If any of the X or arr[i]
// is even
else if (arr[i] % 2 == 0 || X % 2 == 0)
{
// If arr[i] is even
if (arr[i] % 2 == 0)
{
// Add incremented
// value of X to list
X_Values[i]=(X + 1);
// Increment value of
// X permanently
X += 1;
}
else
{
// Incremented value
// of arr[i]
arr[i] += 1;
// Adding current
// value of X to list
X_Values[i]=(X);
}
// Increase the value of
// min_operations variable
min_operation += 1;
}
else
{
// If incrementing one of
// the arr[i] or X gives GCD>1
if (GCD(arr[i] + 1, X) > 1 || GCD(arr[i], X + 1) > 1)
{
// If incrementing arr[i]
// gives GCD>1
if (GCD(arr[i] + 1, X) > 1)
{
// Increment arr[i]
arr[i] += 1;
// Add current Value
// of X to list
X_Values[i]=(X);
}
// If incrementing X gives GCD>1 the
// else part will execute
else
{
// Adding incremented value of X to
// list
X_Values[i]=(X + 1);
// altered value of X permanently
X += 1;
}
// Increment min_operation variable
min_operation += 1;
}
// if incrementing both arr[i] and X gives
// GCD>, Then this else part will execute
else
{
// Incrementing both arr[i] and X
arr[i] += 1;
X_Values[i]=(X + 1);
// Altering X value permanently
X += 1;
// Operation required for this type case
// is 2.Because in first operation arr[i]
// increased and X in second operation.
min_operation += 2;
}
}
}
// Printing value of minimum number
// of operations required
console.log( "Minimum operations required :",min_operation);
// Printing updated arr[]
console.log( "arr[] : ");
for (let i = 0; i < n; i++){
console.log(arr[i]);
}
// Printing values of X for each index
console.log("Values of X : ");
for (let i = 0; i < X_Values.length; i++)
{
console.log(X_Values[i]);
}
// This code is contributed by ksam24000
OutputMinimum 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)
Similar Reads
Minimum changes required to make all Array elements Prime
Given an array of integers arr[], the task is to count the minimum number of changes required to convert each array element to its nearest prime.Examples: Input: arr[] = {4, 25, 13, 6, 20} Output: 5 Explanation: 1 increment required to convert 4 to its nearest prime 5.2 decrements required to conver
9 min read
Minimum increments or decrements by 1 required to make all array elements in AP
Given an array arr[] consisting of N integers, the task is to find the minimum number of increment/decrement by 1 required to be performed on array elements to make all the elements of the given array arr[] in AP. If it is not possible to make the array in AP, then print "-1". Examples: Input: arr[]
11 min read
Count of elements to be multiplied with integers to make each pair of Array a perfect square
Given an array arr[] containing positive integers, the task is to find the minimum number of operations to be done on the array to make every number of the array a superpower. In each operation, we can multiply any element of the array with an integer. A superpower is defined as a number in the arra
15+ min read
Minimize increments required to make differences between all pairs of array elements even
Given an array, arr[] consisting of N integers, the task is to minimize the number of increments of array elements required to make all differences pairs of array elements even. Examples: Input: arr[] = {4, 1, 2}Output: 1Explanation: Operation 1: Increment arr[1] by 1. The array arr[] modifies to {4
5 min read
Minimum prime numbers required to be subtracted to make all array elements equal
Given an array arr[] consisting of N positive integers, the task is to find the minimum number of primes numbers required to be subtracted from the array elements to make all array elements equal. Examples: Input: arr[]= {7, 10, 4, 5}Output: 5Explanation: Following subtraction of primes numbers make
12 min read
Minimize swaps required to make all prime-indexed elements as prime
Given an array arr[] of size N. The task is to find the minimum number of swaps required to re-arrange the array such that all prime-indexed elements are prime, If the task can't be achieved, print "-1" Examples: Input: N = 5, arr[] = {1, 2, 3, 4, 5}Output: 0Explanation: All the prime indices {2, 3,
9 min read
Minimize moves required to make array elements equal by incrementing and decrementing pairs | Set 2
Given an array arr[] of size N, the task is to print the minimum number of moves required to make all array elements equal by selecting any pair of distinct indices and then increment the element at the first index and decrement the element at the other index by 1 each time. If it is impossible to m
7 min read
Minimum number of operations required to make all elements of at least one row of given Matrix prime
Given a matrix, mat[][] of size N * M, the task is to find the minimum count of operations required to make all elements of at least one row of the given matrix prime. In each operation, merge any two rows of the matrix based on the following conditions: If kth elements of both rows of the matrix, i
15 min read
Minimum number of increment / decrements required to be performed on one of the two given numbers to make them non-coprime
Given two positive integers A and B, the task is to find the minimum number of increments/decrements required to be performed on either A or B to make both the numbers non-coprime. Examples: Input: A = 12, B = 3Output: 0Explanation:As 12 & 3 are already non-coprimes, so the minimum count of incr
6 min read
Minimum gcd operations to make all array elements one
Given an array A[] of size N. You can replace any number in the array with the gcd of that element with any of its adjacent elements. Find the minimum number of such operation to make the element of whole array equal to 1. If its not possible print -1. Examples: Input : A[] = {4, 8, 9} Output : 3 Ex
8 min read