Minimum Increment / decrement to make array elements equal
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:
- 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++
#include <bits/stdc++.h>
using namespace std;
int minCost( int A[], int n)
{
int cost = 0;
sort(A, A + n);
int K = A[n / 2];
for ( int i = 0; i < n; ++i)
cost += abs (A[i] - K);
if (n % 2 == 0) {
int tempCost = 0;
K = A[(n / 2) - 1];
for ( int i = 0; i < n; ++i)
tempCost += abs (A[i] - K);
cost = min(cost, tempCost);
}
return cost;
}
int main()
{
int A[] = { 1, 6, 7, 10 };
int n = sizeof (A) / sizeof (A[0]);
cout << minCost(A, n);
return 0;
}
|
Java
import java.util.*;
class GfG {
static int minCost( int A[], int n)
{
int cost = 0 ;
Arrays.sort(A);
int K = A[n / 2 ];
for ( int i = 0 ; i < n; ++i)
cost += Math.abs(A[i] - K);
if (n % 2 == 0 ) {
int tempCost = 0 ;
K = A[(n / 2 ) - 1 ];
for ( int i = 0 ; i < n; ++i)
tempCost += Math.abs(A[i] - K);
cost = Math.min(cost, tempCost);
}
return cost;
}
public static void main(String[] args)
{
int A[] = { 1 , 6 , 7 , 10 };
int n = A.length;
System.out.println(minCost(A, n));
}
}
|
Python3
def minCost(A, n):
cost = 0
A.sort();
K = A[ int (n / 2 )]
for i in range ( 0 , n):
cost = cost + abs (A[i] - K)
if n % 2 = = 0 :
tempCost = 0
K = A[ int (n / 2 ) - 1 ]
for i in range ( 0 , n):
tempCost = tempCost + abs (A[i] - K)
cost = min (cost, tempCost)
return cost
A = [ 1 , 6 , 7 , 10 ]
n = len (A)
print (minCost(A, n))
|
C#
using System;
class GFG {
static int minCost( int []A, int n)
{
int cost = 0;
Array.Sort(A);
int K = A[n / 2];
for ( int i = 0; i < n; ++i)
cost += Math.Abs(A[i] - K);
if (n % 2 == 0) {
int tempCost = 0;
K = A[(n / 2) - 1];
for ( int i = 0; i < n; ++i)
tempCost += Math.Abs(A[i] - K);
cost = Math.Min(cost, tempCost);
}
return cost;
}
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
function minCost( $A , $n )
{
$cost = 0;
sort( $A );
$K = $A [ $n / 2];
for ( $i = 0; $i < $n ; ++ $i )
$cost += abs ( $A [ $i ] - $K );
if ( $n % 2 == 0)
{
$tempCost = 0;
$K = $A [( $n / 2) - 1];
for ( $i = 0; $i < $n ; ++ $i )
$tempCost += abs ( $A [ $i ] - $K );
$cost = min( $cost , $tempCost );
}
return $cost ;
}
$A = array ( 1, 6, 7, 10 );
$n = sizeof( $A );
echo minCost( $A , $n );
?>
|
Javascript
<script>
function minCost(A,n)
{
var cost = 0;
A.sort();
var K = A[parseInt(n/2)];
var i;
for (i = 0; i < n; ++i)
cost += Math.abs(A[i] - K);
if (n % 2 == 0) {
var tempCost = 0;
K = A[parseInt(n / 2) - 1];
for (i = 0; i < n; ++i)
tempCost += Math.abs(A[i] - K);
cost = Math.min(cost, tempCost);
}
return cost;
}
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)
Last Updated :
25 Jul, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...