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.
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.
- Make all array elements equal by repeated subtraction of absolute difference of pairs from their maximum
- Maximum subarray sum in an array created after repeated concatenation
- Minimum array size after repeated replacement of even sum pair with sum
- Print modified array after executing the commands of addition and subtraction
- Making elements of two arrays same with minimum increment/decrement
- Length of array pair formed where one contains all distinct elements and other all same elements
- Find array sum using Bitwise OR after splitting given array in two halves after K circular shifts
- Maximum sum of all elements of array after performing given operations
- Maximize the median of the given array after adding K elements to the same array
- Maximum possible sum of a window in an array such that elements of same window in other array are unique
- Find all unique pairs of maximum and second maximum elements over all sub-arrays in O(NlogN)
- Making all elements of matrix equal to a given element K
- Number of subsequences of maximum length K containing no repeated elements
- Maximum value after merging all elements in the array
- Subsequence pair from given Array having all unique and all same elements respectively
- Find the element before which all the elements are smaller than it, and after which all are greater
- Sum and Maximum of elements in array from [L, R] before and after updates
- Maximize Sum possible by subtracting same value from all elements of a Subarray of the given Array
- Subtraction in the Array
- Range queries for alternatively addition and subtraction on given Array
Improved By : vt_m