Minimum cost to equal all elements of array using two operation
Last Updated :
18 Aug, 2022
Given an array arr[] of n positive integers. There are two operations allowed:
- Operation 1 : Pick any two indexes, increase value at one index by 1 and decrease value at another index by 1. It will cost a.
- Operation 2 : Pick any index and increase its value by 1. It will cost b.
The task is to find the minimum cost to make all the elements equal in the array.
Examples:
Input : n = 4, a = 2, b = 3
arr[] = { 3, 4, 2, 2 }
Output : 5
Perform operation 2 on 3rd index
(0 based indexing). It will cost 2.
Perform operation 1 on index 1 (decrease)
and index 2 (increase). It will cost 3.
Input : n = 3, a = 2, b = 1
arr[] = { 5, 5, 5 }
Output : 0
Approach:
Observe, the final array will not have elements greater than the maximum element of the given array because there is no sense to increase all the elements. Also, they will greater than the minimal element in the original array. Now, iterate over all the possible value of the final array element which needs to be equal and check how many operations of second type must be performed.
For elements to be i (which is one of the possible value of final array element) that number is (n * i - s), where s is the sum of all numbers in the array. The number of operation of the first type for element to be i in the final array can be find by:
for (int j = 0; j < n; j++)
op1 += max(0, a[j] - i)
At the end of each iteration simply check whether the new value ans = (n * i - s) * b + op1 * a is less than previous value of ans. If it is less, update the final ans.
Below is the implementation of above approach.
C++
// CPP Program to find the minimum cost to equal
// all elements of array using two operation
#include <bits/stdc++.h>
using namespace std;
// Return the minimum cost required
int minCost(int n, int arr[], int a, int b)
{
int sum = 0, ans = INT_MAX;
int maxval = 0;
// finding the maximum element and sum of the array.
for (int i = 0; i < n; i++) {
sum += arr[i];
maxval = max(maxval, arr[i]);
}
// for each of the possible value
for (int i = 1; i <= maxval; i++) {
int op1 = 0;
// finding the number of operation 1 required
for (int j = 0; j < n; j++)
op1 += max(0, arr[j] - i);
// finding the minimum cost.
if (sum <= n * i)
ans = min(ans, (n * i - sum) * b + op1 * a);
}
return ans;
}
// Driven Code
int main()
{
int n = 4, a = 2, b = 3;
int arr[] = { 3, 4, 2, 2 };
cout << minCost(n, arr, a, b) << endl;
return 0;
}
Java
// Java Program to find the minimum cost
// to equal all elements of array using
// two operation
import java.lang.*;
class GFG {
// Return the minimum cost required
static int minCost(int n, int arr[],
int a, int b)
{
int sum = 0, ans = Integer.MAX_VALUE;
int maxval = 0;
// finding the maximum element and
// sum of the array.
for (int i = 0; i < n; i++) {
sum += arr[i];
maxval = Math.max(maxval, arr[i]);
}
// for each of the possible value
for (int i = 1; i <= maxval; i++) {
int op1 = 0;
// finding the number of operation
// 1 required
for (int j = 0; j < n; j++)
op1 += Math.max(0, arr[j] - i);
// finding the minimum cost.
if (sum <= n * i)
ans = Math.min(ans, (n * i - sum)
* b + op1 * a);
}
return ans;
}
// Driven Code
public static void main(String [] args)
{
int n = 4, a = 2, b = 3;
int arr[] = { 3, 4, 2, 2 };
System.out.println(minCost(n, arr, a, b));
}
}
// This code is contributed by Smitha.
python3
# Python 3 Program to find the minimum
# cost to equal all elements of array
# using two operation
import sys
# Return the minimum cost required
def minCost(n, arr, a, b):
sum = 0
ans = sys.maxsize
maxval = 0
# finding the maximum element and
# sum of the array.
for i in range(0, n) :
sum += arr[i]
maxval = max(maxval, arr[i])
# for each of the possible value
for i in range(0, n) :
op1 = 0
# finding the number of operation
# 1 required
for j in range(0, n) :
op1 += max(0, arr[j] - i)
# finding the minimum cost.
if (sum <= n * i):
ans = min(ans, (n * i - sum)
* b + op1 * a)
return ans
# Driven Code
n = 4
a = 2
b = 3
arr = [3, 4, 2, 2]
print(minCost(n, arr, a, b))
# This code is contributed by Smitha
C#
// C# Program to find the minimum
// cost to equal all elements of
// array using two operation
using System;
class GFG {
// Return the minimum cost required
static int minCost(int n, int [] arr,
int a, int b)
{
int sum = 0, ans = int.MaxValue;
int maxval = 0;
// finding the maximum element and
// sum of the array.
for (int i = 0; i < n; i++) {
sum += arr[i];
maxval = Math.Max(maxval, arr[i]);
}
// for each of the possible value
for (int i = 1; i <= maxval; i++) {
int op1 = 0;
// finding the number of operation
// 1 required
for (int j = 0; j < n; j++)
op1 += Math.Max(0, arr[j] - i);
// finding the minimum cost.
if (sum <= n * i)
ans = Math.Min(ans, (n * i - sum)
* b + op1 * a);
}
return ans;
}
// Driven Code
public static void Main()
{
int n = 4, a = 2, b = 3;
int []arr= { 3, 4, 2, 2 };
Console.Write(minCost(n, arr, a, b));
}
}
// This code is contributed by Smitha
PHP
<?php
//PHP Program to find the minimum cost
// to equal all elements of array using
// two operation
// Return the minimum cost required
function minCost($n, $arr, $a, $b)
{
$sum = 0; $ans = PHP_INT_MAX;
$maxval = 0;
// finding the maximum element and
// sum of the array.
for ($i = 0; $i < $n; $i++) {
$sum += $arr[$i];
$maxval = max($maxval, $arr[$i]);
}
// for each of the possible value
for ($i = 1; $i <= $maxval; $i++) {
$op1 = 0;
// finding the number of operation
// 1 required
for ($j = 0; $j < $n; $j++)
$op1 += max(0, $arr[$j] - $i);
// finding the minimum cost.
if ($sum <= $n * $i)
$ans = min($ans, ($n * $i - $sum)
* $b + $op1 * $a);
}
return $ans;
}
// Driven Code
$n = 4; $a = 2; $b = 3;
$arr= array(3, 4, 2, 2 );
echo minCost($n, $arr, $a, $b) ,"\n";
// This code is contributed by ajit
?>
JavaScript
<script>
// javascript Program to find the minimum cost
// to equal all elements of array using
// two operation
// Return the minimum cost required
function minCost(n , arr , a , b)
{
var sum = 0, ans = Number.MAX_VALUE;
var maxval = 0;
// finding the maximum element and
// sum of the array.
for (i = 0; i < n; i++) {
sum += arr[i];
maxval = Math.max(maxval, arr[i]);
}
// for each of the possible value
for (i = 1; i <= maxval; i++) {
var op1 = 0;
// finding the number of operation
// 1 required
for (j = 0; j < n; j++)
op1 += Math.max(0, arr[j] - i);
// finding the minimum cost.
if (sum <= n * i)
ans = Math.min(ans, (n * i - sum) * b + op1 * a);
}
return ans;
}
// Driven Code
var n = 4, a = 2, b = 3;
var arr = [ 3, 4, 2, 2 ];
document.write(minCost(n, arr, a, b));
// This code contributed by umadevi9616
</script>
Similar Reads
Minimum Cost to make all array elements equal using given operations
Given an array arr[] of positive integers and three integers A, R, M, where The cost of adding 1 to an element of the array is A,the cost of subtracting 1 from an element of the array is R andthe cost of adding 1 to an element and subtracting 1 from another element simultaneously is M. The task is t
12 min read
Minimum operations to make all elements equal using the second array
Given two arrays A[] and B[] both having N elements. Find the minimum number of operations to make all elements of A equal to the second array B. An operation comprises of: A[i] = A[i] - B[i], 0 <= i <= n-1 Note: If it's not possible to make array elements equal print -1.Examples: Input: A[] =
7 min read
Minimum operation to make all elements equal in array
Given an array consisting of n positive integers, the task is to find the minimum number of operations to make all elements equal. In each operation, we can perform addition, multiplication, subtraction, or division with any number and an array element. Examples: Input : arr[] = [1, 2, 3, 4]Output :
11 min read
Make all Array elements equal to zero in atmost m operations
Given an integer array A[] of size N and integer k. For a fixed value p, choose an index i (1 ⤠i ⤠n) and assign A[i] = max(0, A[i] â p), this counts as one operation, and the task is to find the smallest value of p such that all the elements of the array A[] become 0 in at most k operations. Examp
10 min read
Minimum Bitwise XOR operations to make any two array elements equal
Given an array arr[] of integers of size N and an integer K. One can perform the Bitwise XOR operation between any array element and K any number of times. The task is to print the minimum number of such operations required to make any two elements of the array equal. If it is not possible to make a
9 min read
Make all the elements of equal by using given operation
Given an array A[] of length N, the task is to return the minimum number of times the below-given operations are required to perform so that all elements become equal. If it is not possible then return -1. Choose two distinct indices i and j. Such that A[i] > A[j]Let X = Ceil((A[i] - A[j])/2)Subt
8 min read
Minimum Bitwise OR operations to make any two array elements equal
Given an array arr[] of integers and an integer K, we can perform the Bitwise OR operation between any array element and K any number of times. The task is to print the minimum number of such operations required to make any two elements of the array equal. If it is not possible to make any two eleme
9 min read
Minimum delete operations to make all elements of array same
Given an array of n elements such that elements may repeat. We can delete any number of elements from the array. The task is to find a minimum number of elements to be deleted from the array to make it equal.Examples: Input: arr[] = {4, 3, 4, 4, 2, 4} Output: 2 After deleting 2 and 3 from array, arr
10 min read
Maximize minimum element of an Array using operations
Given an array A[] of N integers and two integers X and Y (X ⤠Y), the task is to find the maximum possible value of the minimum element in an array A[] of N integers by adding X to one element and subtracting Y from another element any number of times, where X ⤠Y. Examples: Input: N= 3, A[] = {1,
8 min read
Minimum Bitwise AND operations to make any two array elements equal
Given an array of integers of size 'n' and an integer 'k', We can perform the Bitwise AND operation between any array element and 'k' any number of times. The task is to print the minimum number of such operations required to make any two elements of the array equal. If it is not possible to make an
10 min read