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.
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.
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:
8 1 0
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Generate original array from an array that store the counts of greater elements on right
- Minimum number greater than the maximum of array which cannot be formed using the numbers in the array
- Find minimum value to assign all array elements so that array product becomes greater
- Count elements in first Array with absolute difference greater than K with an element in second Array
- Count of elements in first Array greater than second Array with each element considered only once
- Find K such that changing all elements of the Array greater than K to K will make array sum N
- Count the number of sub-arrays such that the average of elements present in the sub-array is greater than that not present in the sub-array
- Count of Array elements greater than or equal to twice the Median of K trailing Array elements
- Smallest greater elements in whole array
- Find next Smaller of next Greater in an array
- Sum of all array elements less than X and greater than Y for Q queries
- Rearrange array such that even positioned are greater than odd
- Number of pairs in an array with the sum greater than 0
- Check if a subarray exists with sum greater than the given Array
- Find the next greater element in a Circular Array
- Minimum removals from array to make GCD greater
- Count the values greater than X in the modified array
- Find closest greater value for every element in array
- Smallest subarray from a given Array with sum greater than or equal to K
- Number of pairs in an array such that product is greater than sum
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : vt_m