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
- 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
- Generate original array from an array that store the counts of greater elements on right
- 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
- Find next Smaller of next Greater in an array
- Rearrange array such that even positioned are greater than odd
- Smallest greater elements in whole array
- Minimum removals from array to make GCD greater
- Elements greater than the previous and next element in an Array
- Number of pairs in an array such that product is greater than sum
- Smallest element greater than X not present in the array
- Find closest greater value for every element in array
- Count the values greater than X in the modified array
- Closest greater or same value on left side for every element in array
- Rearrange an array such that every odd indexed element is greater than it previous
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