Input : arr = [1, 6, 1, 1, 1];
Output : 4
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 a temporary variable for store the number of operations performed.
- Find the maximum element of the given array and store its index value.
- Check if all elements are equal to the maximum element after n subtractions.
- Again check that each element is equal to the other elements and return n.
Below is the implementation of the above approach: