Given an array of N integers, the task is to find the number of operations required to make all elements in the array equal. In one operation we can distribute equal weights from the maximum element to the rest of the array elements. If it is not possible to make the array elements equal after performing the above operations then print -1.
Input: arr = [1, 6, 1, 1, 1];
Explanation: Since arr becomes [2, 2, 2, 2, 2] after distribution from max element.
Input : arr = [2, 2, 3];
Output : -1
Explanation: Here arr becomes [3, 3, 1] after distribution.
- Declare temporary variable to store number of times operation is performed.
- Find maximum element of the given array and store its index value.
- Check if all the elements are equal to the maximum element after n subtractions.
- Again check that each element is equal to other elements and return n.
Below is the implementation of above approach:
Time complexity: O(n)
- Find the minimum number of operations required to make all array elements equal
- Minimum operations required to make all the array elements equal
- Find if it is possible to make all elements of an array equal by the given operations
- Minimum operations required to make all the elements distinct in an array
- Minimum operations required to make all Array elements divisible by K
- Minimum no. of operations required to make all Array Elements Zero
- Minimum number of increment-other operations to make all array elements equal.
- Minimize operations required to make each element of Array equal to it's index value
- Minimum operations to make all elements equal using the second array
- Count of operations to make all elements of array a equal to its min element by performing a[i] – b[i]
- Minimum Cost to make all array elements equal using given operations
- Minimum increment by k operations to make all elements equal
- Minimum changes required to make all element in an array equal
- Count of replacements required to make the sum of all Pairs of given type from the Array equal
- Minimum Bitwise AND operations to make any two array elements equal
- Minimum Bitwise OR operations to make any two array elements equal
- Minimum Bitwise XOR operations to make any two array elements equal
- Minimum decrement operations to make Array elements equal by only decreasing K each time
- Minimum number of operations on an array to make all elements 0
- Find the minimum number of preprocess moves required to make two strings equal
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.