The Wayback Machine - https://web.archive.org/web/20220314134445/https://www.geeksforgeeks.org/minimize-increment-or-simultaneous-increment-and-decrement-to-make-all-array-elements-equal/
Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Minimize increment or simultaneous increment and decrement to make all Array elements equal

  • Last Updated : 14 Mar, 2022

Given an array arr[] of size N, the task is to minimize the number of steps to make all the array elements equal by performing the following operations:

  1. Choose an element of the array and increase it by 1.
  2. Choose two elements simultaneously (arr[i], arr[j]) increase arr[i] by 1 and decrease arr[j] by 1.

Examples:

Input: arr = [4, 2, 4, 6]
Output: 2
Explanation: Do operation 2 on element 2 and 6 of the array. 
Hence increasing 2 by 2 and decreasing 6 by 2 makes all the elements of the array equal.

Input: arr = [1, 2, 3, 4]
Output: 3
Explanation: Increase 1 by 1 once. arr[] = {2, 2, 3, 4}.
Then increase 1 by 1 and decrease 4 by 1 in one step. arr[] = {3, 2, 3, 3}
Increase 2 by 1. arr[] = {3, 3, 3, 3}. So total operations = 3.

 

Approach: This problem can be solved using the greedy approach based on the following idea:

To minimize the number of steps make all of them equal to the ceil(average) of all array elements. To do this simultaneously increase the less elements and decrement the greater elements as long as possible and then increment the lesser elements only.

Follow the steps mentioned below to solve the problem:

  • Sort the array.
  • Find out the ceil of the average of the array (say avg).
  • Now traverse the array from i = 0 to N-1:
    • whenever you find any element smaller than avg.
      • Traverse from the back end of the array and deduce the elements which are greater than avg.
      • Finally, add avg-a[i] to the answer.
  • Return the answer.

Below is the implementation of the above approach:

C++




// C++ code to implement the approach
  
#include <bits/stdc++.h>
#define ll long long
#define mod 1000000007
using namespace std;
  
// function to find the minimum operations
// to make the array elements same
int findMinOperations(vector<int> a, int n)
{
    ll avg = 0;
  
    // Sorting the array
    sort(a.begin(), a.end());
    for (int i : a) {
        avg += i;
    }
  
    // Finding out the average
  
    avg = avg % n == 0 ? avg / n : avg / n + 1;
    int i = 0, j = n - 1;
    int ans = 0;
  
    // Traversing the array
    while (i <= j) {
  
        // If current element is less than avg
        if (a[i] < avg) {
  
            // Total increments needed
            int incrementNeeded = avg - a[i];
            int k = incrementNeeded;
            a[i] = avg;
  
            // Traversing in the right side
            // of the array to find elements
            // which needs to be deduced to avg
            while (j > i && k > 0
                   && a[j] > avg) {
                int decrementNeeded
                    = a[j] - avg;
                if (k <= decrementNeeded) {
                    k -= decrementNeeded;
                }
                else {
                    a[j] -= k;
                    k = 0;
                }
                j--;
            }
  
            // Adding increments
            // needed to ans
            ans += incrementNeeded;
        }
        i++;
    }
    return ans;
}
  
// Driver Code
int main()
{
    vector<int> A;
    A = { 1, 2, 3, 4 };
    int N = A.size();
    cout << findMinOperations(A, N);
    return 0;
}
Output
3

Time Complexity: O(N)
Auxiliary Space: O(1)


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!