VOOZH about

URL: https://www.geeksforgeeks.org/dsa/minimum-number-increment-operations-make-array-elements-equal/

⇱ Minimum number of increment-other operations to make all array elements equal. - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Minimum number of increment-other operations to make all array elements equal.

Last Updated : 16 May, 2025

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

[Naive Approach] Using Sorting – O(n^2logn) Time and O(1) Space

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.


Output
3

[Expected Approach] Using Direct Formula – O(n) Time and O(1) Space

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 subtracts n * smallest element from the sum to find the minimum number of operations.


Output
2
Comment
Article Tags:
Article Tags: