Related Articles
GCD of array is greater than one
• Difficulty Level : Medium
• Last Updated : 13 Apr, 2018

Given an array of n integers. The array is considered best if GCD of all its elements is greater than 1. If the array is not best, we can choose an index i (1 <= i < n) and replace numbers ai and ai + 1 by ai – ai + 1 and ai + ai + 1 respectively. Find the minimum number of operations to be done on the array to make it best.

Examples:

```Input : n = 2
a[] = [1, 1]
Output : 1
Explanation:
Here, gcd(1,1) = 1. So, to make
it best we have to replace array
by [(1-1), (1+1)] = [0, 2]. Now,
gcd(0, 2) > 1. Hence, in one
operation array become best.

Input : n = 3
a[] = [6, 2, 4]
Output :0
Explanation:
Here, gcd(6,2,4) > 1.
Hence, no operation is required.
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

We first calculate the gcd(array) and check whether it is greater than 1. If yes then the array is already best else we greedily check for no. of moves required to make all ones by using the property that when there are two odd numbers then you can make them even in one move else if there is one odd and one even then you require two moves.

Below is the implementation of the above approach:

## C++

 `// CPP program to find bestArray``#include``using` `namespace` `std;`` ` `// Calculating gcd of two numbers``int` `gcd(``int` `a, ``int` `b){``        ``if` `(a == 0)``            ``return` `b;``        ``return` `gcd(b%a, a);``}`` ` `void` `bestArray(``int` `arr[], ``int` `n){``        ``bool` `even[n] = {``false``};``        ``int` `ans = 0;`` ` `        ``// calculating gcd and``        ``// counting the even numbers``        ``for``(``int` `i = 0; i < n; i++){``            ``ans = gcd(ans, arr[i]);``            ``if``(arr[i] % 2 == 0)``                ``even[i] = ``true``;``        ``}`` ` `        ``// check array is already best``        ``if``(ans > 1)``            ``cout << 0 << endl;``        ``else``{`` ` `            ``// counting the number ``            ``// of operations required.``            ``ans = 0;``            ``for``(``int` `i = 0; i < n-1; i++){``                ``if``(!even[i]){``                    ``even[i] = ``true``;``                    ``even[i+1] = ``true``;``                    ``if``(arr[i+1]%2 != 0){``                        ``ans+=1;``                    ``}``                    ``else``                        ``ans+=2;``                ``}``            ``}``            ``if``(!even[n-1]){``                ``ans+=2;``            ``}``            ``cout << ans << endl;``        ``}``}`` ` `// driver code``int` `main(){``     ` `        ``int` `arr[] = {57, 30, 28, 81, 88, 32, 3, 42, 25};``        ``int` `n = 9;``        ``bestArray(arr, n);`` ` `        ``int` `arr1[] = {1, 1};``        ``n = 2;``        ``bestArray(arr1, n);`` ` `        ``int` `arr2[] = {6, 2, 4};``        ``n = 3;``        ``bestArray(arr2, n);``}`` ` `/*This code is contributed by Sagar Shukla.*/`

## Java

 `// Java code to find best array``import` `java.util.*;``import` `java.lang.*;`` ` `public` `class` `GeeksforGeeks{``     ` `    ``// function to calculate gcd of two numbers.``    ``public` `static` `int` `gcd(``int` `a, ``int` `b){``        ``if` `(a == ``0``)``            ``return` `b;``        ``return` `gcd(b%a, a);``    ``}`` ` `    ``public` `static` `void` `bestArray(``int` `arr[], ``int` `n){``        ``boolean` `even[] = ``new` `boolean``[n];``        ``int` `ans = ``0``;``        ``for``(``int` `i=``0``; i ``1``)``            ``System.out.println(``0``);``        ``else``{``             ` `            ``// counting the number of operations required.``            ``ans = ``0``;``            ``for``(``int` `i=``0``; i

## Python

 `# code to find the best array``from` `fractions ``import` `gcd`` ` `def` `bestArray(a,n):`` ` `    ``even ``=` `[``False``]``*``n``    ``ans ``=` `0`` ` `    ``# calculating the gcd and``    ``# counting the even numbers``    ``for` `i ``in` `xrange``(n):``        ``ans ``=` `gcd(ans, a[i])``        ``if` `a[i]``%``2` `=``=` `0``:``            ``even[i] ``=` `True`` ` `    ``# check if array is already best.``    ``if` `ans > ``1``:``        ``print` `(``0``)``    ``else``:       `` ` `        ``# calculating the no of``        ``# operations required.``        ``ans ``=` `0` `        ``for` `i ``in` `xrange``(n``-``1``):``            ``if` `not` `even[i]:``                ``even[i] ``=` `True``                ``even[i``+``1``] ``=` `True``                ``if` `a[i``+``1``]``%``2` `!``=` `0``: ``                    ``ans ``+``=` `1``                ``else``:``                    ``ans ``+``=` `2``        ``if` `not` `even[n``-``1``]:``            ``ans ``+``=` `2``        ``print` `(ans)`` ` `# driver code``n ``=` `9``a ``=` `[``57``, ``30``, ``28``, ``81``, ``88``, ``32``, ``3``, ``42``, ``25``]``bestArray(a,n)``n ``=` `2``a ``=` `[``1``, ``1``]``bestArray(a,n)``n ``=` `3``a ``=` `[``6``, ``2``, ``4``]``bestArray(a,n)`

## C#

 `// C# code to find best array``using` `System;`` ` `public` `class` `GFG {``     ` `    ``// function to calculate gcd``    ``// of two numbers.``    ``public` `static` `int` `gcd(``int` `a, ``int` `b)``    ``{``        ``if` `(a == 0)``            ``return` `b;``             ` `        ``return` `gcd(b % a, a);``    ``}`` ` `    ``public` `static` `void` `bestArray(``int` `[]arr, ``int` `n)``    ``{``        ``bool` `[]even = ``new` `bool``[n];``        ``int` `ans = 0;``         ` `        ``for``(``int` `i = 0; i < n; i++)``            ``even[i] = ``false``;`` ` `        ``// calculating gcd and ``        ``// counting the even numbers``        ``for``(``int` `i = 0; i < n; i++)``        ``{``            ``ans = gcd(ans, arr[i]);``             ` `            ``if``(arr[i] % 2 == 0)``                ``even[i] = ``true``;``        ``}``         ` `        ``// check array is already best``        ``if``(ans > 1)``            ``Console.WriteLine(0);``        ``else``        ``{``             ` `            ``// counting the number of ``            ``// operations required.``            ``ans = 0;``            ``for``(``int` `i = 0; i < n-1; i++)``            ``{``                ``if``(!even[i])``                ``{``                    ``even[i] = ``true``;``                    ``even[i+1] = ``true``;``                    ``if``(arr[i+1] % 2 != 0)``                        ``ans += 1;``                    ``else``                        ``ans += 2;``                ``}``            ``}``            ``if``(!even[n-1])``                ``ans += 2;``                 ` `            ``Console.WriteLine(ans);``        ``}``    ``}``     ` `    ``// driver code``    ``public` `static` `void` `Main()``    ``{``         ` `        ``int` `[]arr = {57, 30, 28, 81, 88,``                            ``32, 3, 42, 25};``        ``int` `n = 9;``        ``bestArray(arr, n);``         ` `        ``int` `[]arr1 = {1, 1};``        ``n = 2;``        ``bestArray(arr1, n);``         ` `        ``int` `[]arr2 = {6, 2, 4};``        ``n = 3;``        ``bestArray(arr2, n);``    ``}``}`` ` `// This code is contributed by vt_m.`

Output:
```8
1
0
```

My Personal Notes arrow_drop_up