Minimum Increment / decrement to make array elements equal
Last Updated :
25 Jul, 2022
Given an array of integers where 1 \leq A[i] \leq 10^{18} . 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:
- Sort the array of Integers in increasing order.
- 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 elements, we will have to check for the costs of both middle elements and take the minimum. - If A[i] < K, Increment the element by K - A[i].
- If A[i] > K, Decrement the element by A[i] - K.
- 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 <bits/stdc++.h>
using namespace std;
// Function to return minimum operations need
// to be make each element of array equal
int 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 Code
int main()
{
int A[] = { 1, 6, 7, 10 };
int n = sizeof(A) / sizeof(A[0]);
cout << minCost(A, n);
return 0;
}
Java
// Java program to find minimum Increment or
// decrement to make array elements equal
import java.util.*;
class GfG {
// Function to return minimum operations need
// to be make each element of array equal
static 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 Code
public 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 equal
def 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 code
A = [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 equal
using System;
class GFG {
// Function to return minimum operations need
// to be make each element of array equal
static 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 Code
public static void Main(String[] args)
{
int []A = new int []{ 1, 6, 7, 10 };
int n = A.Length;
Console.WriteLine(minCost(A, n));
}
}
PHP
<?php
// PHP 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 equal
function minCost($A, $n)
{
// Initialize cost to 0
$cost = 0;
// Sort the array
sort($A);
// Middle element
$K = $A[$n / 2];
// Find Cost
for ($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)
{
$tempCost = 0;
$K = $A[($n / 2) - 1];
// Find cost again
for ($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 Code
$A = array( 1, 6, 7, 10 );
$n = sizeof($A);
echo minCost($A, $n);
// This code is contributed
// by Sach_Code
?>
JavaScript
<script>
// Javascript 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 equal
function minCost(A,n)
{
// Initialize cost to 0
var cost = 0;
// Sort the array
A.sort();
// Middle element
var K = A[parseInt(n/2)];
var i;
// Find Cost
for (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) {
var tempCost = 0;
K = A[parseInt(n / 2) - 1];
// Find cost again
for (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 Code
var A = [1, 6, 7, 10];
var n = A.length;
document.write(minCost(A, n));
</script>
Time Complexity: O(N*log(N)), Auxiliary Space: O(1)
Further Optimization We can find median in linear time and reduce the time complexity to O(N)
Similar Reads
Minimum cost to make Array equal by increment/decrementing elements Given an array, arr[], and the cost array cost[], the task is to find the minimum cost to make all the array elements equal by incrementing or decrementing the element at any index by 1. The cost of the increment or decrement operation for the element at index i is the cost[i]. Examples: Input: arr[
8 min read
Minimize moves to make Array elements equal by incrementing and decrementing pairs Given an array arr[] of size N, the task is to print the minimum number of moves needed to make all array elements equal by selecting any two distinct indices and then increment the element at the first selected index and decrement the element at the other selected index by 1 in each move. If it is
14 min read
Making elements of two arrays same with minimum increment/decrement Given two arrays of same size, we need to convert the first array into another with minimum operations. In an operation, we can either increment or decrement an element by one. Note that orders of appearance of elements do not need to be same. Here to convert one number into another we can add or su
6 min read
Minimum increment/decrement to make array non-Increasing Given an array a, your task is to convert it into a non-increasing form such that we can either increment or decrement the array value by 1 in the minimum changes possible. Examples : Input : a[] = {3, 1, 2, 1}Output : 1Explanation : We can convert the array into 3 1 1 1 by changing 3rd element of a
11 min read
Minimum cost to make every Kth element in Array equal Given an array arr[] of integers and an integer K, the task is to find the minimum number of operations required to make every Kth element in the array equal. While performing one operation you can either increase a number by one or decrease the number by one. Examples: Input: arr[] = {1, 2, 3, 4, 4
6 min read