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.
- Length of longest subarray in which elements greater than K are more than elements not greater than K
- Smallest subarray of size greater than K with sum greater than a given value
- Largest subsequence having GCD greater than 1
- Smallest divisor D of N such that gcd(D, M) is greater than 1
- Split N natural numbers into two sets having GCD of their sums greater than 1
- Smallest subsequence having GCD equal to GCD of given array
- Sum of all array elements less than X and greater than Y for Q queries
- Pair of integers having least GCD among all given pairs having GCD exceeding K
- Minimum removals from array to make GCD greater
- Minimum gcd operations to make all array elements one
- Maximum possible GCD after replacing at most one element in the given array
- Finding LCM of more than two (or array) numbers without using GCD
- C++ Program for GCD of more than two (or array) numbers
- Java Program for GCD of more than two (or array) numbers
- GCD of more than two (or array) numbers
- Minimum number greater than the maximum of array which cannot be formed using the numbers in the array
- 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 K such that changing all elements of the Array greater than K to K will make array sum N
- Number of indices pair such that element pair sum from first Array is greater than second Array
- Count of elements in first Array greater than second Array with each element considered only once
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