GFG App
Open App
Browser
Continue

# 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++

 `// C++ code to implement the approach` `#include ` `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 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`

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)

My Personal Notes arrow_drop_up