Skip to content
Related Articles
Minimum Increment / decrement to make array elements equal
• Difficulty Level : Medium
• Last Updated : 26 Apr, 2021

Given an array of integers where . In one operation you can either Increment/Decrement any element by 1. The task is to find the minimum operations needed to be performed on the array elements to make all array elements equal.
Examples

Input : A[] = { 1, 5, 7, 10 }
Output : 11
Increment 1 by 4, 5 by 0.
Decrement 7 by 2, 10 by 5.
New array A = { 5, 5, 5, 5 } with
cost of operations = 4 + 0 + 2 + 5 = 11.

Input : A = { 10, 2, 20 }
Output : 18

Approach:

1. Sort the array of Integers in increasing order.
2. Now, to make all elements equal with min cost. We will have to make the elements equal to the middle element of this sorted array. So, select the middle value, Let it be K.
Note: In case of even numbers of element, we will have to check for the costs of both middle elements and take minimum.
3. If A[i] < K, Increment the element by K – A[i].
4. If A[i] > K, Decrement the element by A[i] – K.
5. Update cost of each operation performed.

Below is the implementation of above approach:

## C++

 // C++ program to find minimum Increment or// decrement to make array elements equal#include using namespace std; // Function to return minimum operations need// to be make each element of array equalint minCost(int A[], int n){    // Initialize cost to 0    int cost = 0;     // Sort the array    sort(A, A + n);     // Middle element    int K = A[n / 2];     // Find Cost    for (int i = 0; i < n; ++i)        cost += abs(A[i] - K);     // If n, is even. Take minimum of the    // Cost obtained by considering both    // middle elements    if (n % 2 == 0) {        int tempCost = 0;         K = A[(n / 2) - 1];         // Find cost again        for (int i = 0; i < n; ++i)            tempCost += abs(A[i] - K);         // Take minimum of two cost        cost = min(cost, tempCost);    }     // Return total cost    return cost;} // Driver Codeint main(){    int A[] = { 1, 6, 7, 10 };     int n = sizeof(A) / sizeof(A);     cout << minCost(A, n);     return 0;}

## Java

 // Java program to find minimum Increment or// decrement to make array elements equalimport java.util.*;class GfG { // Function to return minimum operations need// to be make each element of array equalstatic int minCost(int A[], int n){    // Initialize cost to 0    int cost = 0;     // Sort the array    Arrays.sort(A);     // Middle element    int K = A[n / 2];     // Find Cost    for (int i = 0; i < n; ++i)        cost += Math.abs(A[i] - K);     // If n, is even. Take minimum of the    // Cost obtained by considering both    // middle elements    if (n % 2 == 0) {        int tempCost = 0;         K = A[(n / 2) - 1];         // Find cost again        for (int i = 0; i < n; ++i)            tempCost += Math.abs(A[i] - K);         // Take minimum of two cost        cost = Math.min(cost, tempCost);    }     // Return total cost    return cost;} // Driver Codepublic static void main(String[] args){    int A[] = { 1, 6, 7, 10 };     int n = A.length;     System.out.println(minCost(A, n));}}

## Python3

 # Python3 program to find minimum Increment or# decrement to make array elements equal     # Function to return minimum operations need# to be make each element of array equaldef minCost(A, n):         # Initialize cost to 0    cost = 0         # Sort the array    A.sort();         # Middle element    K = A[int(n / 2)]         #Find Cost    for i in range(0, n):        cost = cost + abs(A[i] - K)         # If n, is even. Take minimum of the    # Cost obtained by considering both    # middle elements    if n % 2 == 0:        tempCost = 0        K = A[int(n / 2) - 1]                 # FInd cost again        for i in range(0, n):            tempCost = tempCost + abs(A[i] - K)                 # Take minimum of two cost        cost = min(cost, tempCost)             # Return total cost    return cost     # Driver codeA = [1, 6, 7, 10]n = len(A) print(minCost(A, n))         # This code is contributed# by Shashank_Sharma

## C#

 // C# program to find minimum Increment or// decrement to make array elements equalusing System; class GFG {     // Function to return minimum operations need// to be make each element of array equalstatic int minCost(int []A, int n){    // Initialize cost to 0    int cost = 0;     // Sort the array    Array.Sort(A);     // Middle element    int K = A[n / 2];     // Find Cost    for (int i = 0; i < n; ++i)        cost += Math.Abs(A[i] - K);     // If n, is even. Take minimum of the    // Cost obtained by considering both    // middle elements    if (n % 2 == 0) {        int tempCost = 0;         K = A[(n / 2) - 1];         // Find cost again        for (int i = 0; i < n; ++i)            tempCost += Math.Abs(A[i] - K);         // Take minimum of two cost        cost = Math.Min(cost, tempCost);    }     // Return total cost    return cost;} // Driver Codepublic static void Main(String[] args){    int []A = new int []{ 1, 6, 7, 10 };     int n = A.Length;     Console.WriteLine(minCost(A, n));}}

## PHP

 

## Javascript

 
Output:

10

Time Complexity: O(N*log(N))
Further Optimization We can find median in linear time and reduce time complexity to O(N)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with industry experts, please refer DSA Live Classes

My Personal Notes arrow_drop_up