Given an array of n elements, find out the maximum possible sum of all elements such that all the elements are equal. Only operation allowed is choosing any two elements and replacing the larger of them by the absolute difference of the two.
Input : 9 12 3 6 Output : 12 Explanation : 9 12 3 6 replace a2 = 12 with a2-a4 = 12 - 6 => 6 i.e, 9 6 3 6 replace a4 = 6 with a4-a3 = 6 - 3 => 3 i.e, 9 6 3 3 replace a1 = 9 with a1-a2 = 9 - 6 => 3 i.e, 3 6 3 3 replace a2 = 6 with a2-a4 = 6 - 3 => 3 i,e. 3 3 3 3 Now, at this point we have all the elements equal, hence we can return our answer from here. Input : 4 8 6 10 Output : 8 Explanation : Resultant array formed will be: 4 8 6 10 replace a4 = 10 with a4-a1 = 10 - 4 => 6 i.e, 4 8 6 6 replace a3 = 6 with a3-a1 = 6 - 4 => 2 i.e, 4 8 2 6 replace a2 = 8 with a2-a4 = 8 - 6 => 2 i.e, 4 2 2 6 replace a4 = 6 with a4-a1 = 6 - 4 => 2 i,e. 4 2 2 2 replace a1 = 4 with a1-a2 = 4 - 2 => 2 i,e. 2 2 2 2 Now, at this point we have all the elements equal, hence we can return our answer from here.
By analyzing the given operation i.e,
ai = ai - aj where ai > aj
We see that this is similar to finding GCD through Euclidean algorithm as:
GCD(a, b) = GCD(b, a - b)
And also, the order of rearrangement does not matter, we can proceed by taking any two elements and replace the larger value by the absolute difference of the two, and repeat among them till the difference comes out to be zero[both the elements be same]. That is, taking out the GCD of any two numbers. The reason for this to work is, GCD is associative and commutative.
So the idea is the take the GCD of all the elements at once and replace all the elements by that result.
This article is contributed by Shubham Gupta. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Number of subsequences of maximum length K containing no repeated elements
- Find maximum xor of k elements in an array
- Making elements distinct in a sorted array by minimum increments
- Find the maximum elements in the first and the second halves of the Array
- Find a number that divides maximum array elements
- Find all numbers that divide maximum array elements
- Maximum subarray sum in an array created after repeated concatenation
- Find integers that divides maximum number of elements of the array
- Find maximum points which can be obtained by deleting elements from array
- Recursive Programs to find Minimum and Maximum elements of array
- Find top three repeated in array
- Sum of elements in 1st array such that number of elements less than or equal to them in 2nd array is maximum
- Find all unique pairs of maximum and second maximum elements over all sub-arrays in O(NlogN)
- Subtraction in the Array
- Maximum in array which is at-least twice of other elements
Improved By : vt_m