![]() |
VOOZH | about |
We are given an array consisting of n elements. At each operation we can select any one element and increase rest of n-1 elements by 1. We have to make all elements equal performing such operation as many times you wish. Find the minimum number of operations needed for this.
Examples:
Input: arr[] = [1, 2, 3]
Output: 3
Explanation:
Operation 1 : Increase all except 3rd, we get 2, 3, 3
Operation 2 : Increase all except 3rd, we get 3, 4, 3
Operation 3 : Increase all except 2nd, we get 4, 4, 4
Input: arr[] = [4, 3, 4]
Output: 2
Explanation:
Operation 1 : Increase all except 3rd, we get 5, 4, 4
Operation 2 : Increase all except 1st, we get 5, 5, 5
One important observation is, the order of operations does not matter, we always get the same count. For example, in the first case. If we reorder the operations, we end up incrementing every value by same amount.
The naive approach increments all elements except the maximum one in each step until all elements become equal..
The array is modified in-place, and no extra space is used other than the input array.
3
If we took a closer look at each operation as well problem statement we will find that increasing all n-1 element except the largest one is similar to decreasing the largest element only. So, the smallest elements need not to decrease any more and rest of elements will got decremented upto the smallest one.
The optimized approach calculates the total sum of the array and identifies the smallest element.
It subtractsn * smallest elementfrom the sum to find the minimum number of operations.
2