# 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 and
• the cost of adding 1 to an element and subtracting 1 from another element simultaneously is M.

The task is to find the minimum total cost to make all the elements of the array equal.

Examples:

Input: arr[] = {5, 5, 3, 6, 5}, A = 1, R = 2, M = 4
Output: 4
Explanation:
Operation 1: Add two times to element 3, Array – {5, 5, 5, 6, 5}, Cost = 2
Operation 2: Substract one time to element 6, Array – {5, 5, 5, 5, 5}, Cost = 4
Therefore, minimum cost is 4.

Input: arr[] = {5, 5, 3, 6, 5}, A = 1, R = 2, M = 2
Output: 3

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach: The idea is to:

1. find the minimum of the M and A + R as M can make both operations simultaneously.
2. Then, store prefix sum in an array to find the sum in constant time.
3. Now for each element calculate the cost of making equal to the current element and find the minimum of them.
4. The smallest answer can also exist when we make all elements equal to the average of the array.
5. Therefore, at the end also compute the cost for making all elements equal to approximate average sum of elements.

Below is the implementation of above approach:

## C++

 `// C++ implementation to find the ` `// minimum cost to make all array ` `// elements equal ` ` `  `#include ` `using` `namespace` `std; ` ` `  `// Function that returns the cost of ` `// makeing all elements equal to current element ` `int` `costCalculation( ` `    ``int` `current, ``int` `arr[], ` `    ``int` `n, ``int` `pref[], ` `    ``int` `a, ``int` `r, ` `    ``int` `minimum) ` `{ ` ` `  `    ``// Compute the lower bound ` `    ``// of current element ` `    ``int` `index ` `        ``= lower_bound( ` `              ``arr, arr + n, current) ` `          ``- arr; ` ` `  `    ``// Calculate the requirement ` `    ``// of add operation ` `    ``int` `left ` `        ``= index * current - pref[index]; ` ` `  `    ``// Calcuate the requirement ` `    ``// of subtract operation ` `    ``int` `right ` `        ``= pref[n] - pref[index] ` `          ``- (n - index) ` `                ``* current; ` ` `  `    ``// Compute minimum of left and right ` `    ``int` `res = min(left, right); ` `    ``left -= res; ` `    ``right -= res; ` ` `  `    ``// Computing the total cost of add ` `    ``// and subtract operations ` `    ``int` `total = res * minimum; ` `    ``total += left * a; ` `    ``total += right * r; ` ` `  `    ``return` `total; ` `} ` ` `  `// Function that prints minimum cost ` `// of making all elements equal ` `void` `solve(``int` `arr[], ``int` `n, ` `           ``int` `a, ``int` `r, ``int` `m) ` `{ ` `    ``// Sort the given array ` `    ``sort(arr, arr + n); ` ` `  `    ``// Calcuate minimum from a + r and m ` `    ``int` `minimum = min(a + r, m); ` ` `  `    ``int` `pref[n + 1] = { 0 }; ` ` `  `    ``// Compute prefix sum ` `    ``// and store in pref ` `    ``// array ` `    ``for` `(``int` `i = 0; i < n; i++) ` `        ``pref[i + 1] = pref[i] + arr[i]; ` ` `  `    ``int` `ans = 10000; ` ` `  `    ``// Find the minimum cost ` `    ``// from the given elements ` `    ``for` `(``int` `i = 0; i < n; i++) ` `        ``ans ` `            ``= min( ` `                ``ans, ` `                ``costCalculation( ` `                    ``arr[i], arr, n, ` `                    ``pref, a, r, ` `                    ``minimum)); ` ` `  `    ``// Finding the minimum cost ` `    ``// from the other cases where ` `    ``// minimum cost can occur ` `    ``ans ` `        ``= min( ` `            ``ans, ` `            ``costCalculation( ` `                ``pref[n] / n, arr, ` `                ``n, pref, a, ` `                ``r, minimum)); ` `    ``ans ` `        ``= min( ` `            ``ans, ` `            ``costCalculation( ` `                ``pref[n] / n + 1, ` `                ``arr, n, pref, ` `                ``a, r, minimum)); ` ` `  `    ``// Printing the minimum cost of making ` `    ``// all elements equal ` `    ``cout << ans << ``"\n"``; ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``int` `arr[] = { 5, 5, 3, 6, 5 }; ` ` `  `    ``int` `A = 1, R = 2, M = 4; ` ` `  `    ``int` `size = ``sizeof``(arr) / ``sizeof``(arr); ` ` `  `    ``// Function Call ` `    ``solve(arr, size, A, R, M); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java implementation to find the ` `// minimum cost to make all array ` `// elements equal ` `import` `java.lang.*; ` `import` `java.util.*; ` ` `  `class` `GFG{ ` ` `  `public` `static` `int` `lowerBound(``int``[] array, ``int` `length,  ` `                                          ``int` `value) ` `{ ` `    ``int` `low = ``0``; ` `    ``int` `high = length; ` `     `  `    ``while` `(low < high) ` `    ``{ ` `        ``final` `int` `mid = (low + high) / ``2``; ` `         `  `        ``// Checks if the value is less  ` `        ``// than middle element of the array ` `        ``if` `(value <= array[mid]) ` `        ``{ ` `            ``high = mid; ` `        ``} ` `        ``else` `        ``{ ` `            ``low = mid + ``1``; ` `        ``} ` `    ``} ` `    ``return` `low; ` `} ` ` `  `// Function that returns the cost of makeing  ` `// all elements equal to current element ` `public` `static` `int` `costCalculation(``int` `current, ``int` `arr[], ` `                                  ``int` `n, ``int` `pref[], ` `                                  ``int` `a, ``int` `r,  ` `                                  ``int` `minimum) ` `{ ` ` `  `    ``// Compute the lower bound ` `    ``// of current element ` `    ``int` `index = lowerBound(arr, arr.length, current); ` ` `  `    ``// Calculate the requirement ` `    ``// of add operation ` `    ``int` `left = index * current - pref[index]; ` ` `  `    ``// Calcuate the requirement ` `    ``// of subtract operation ` `    ``int` `right = pref[n] -  ` `                ``pref[index]- (n - index)* current; ` ` `  `    ``// Compute minimum of left and right ` `    ``int` `res = Math.min(left, right); ` `    ``left -= res; ` `    ``right -= res; ` ` `  `    ``// Computing the total cost of add ` `    ``// and subtract operations ` `    ``int` `total = res * minimum; ` `    ``total += left * a; ` `    ``total += right * r; ` ` `  `    ``return` `total; ` `} ` ` `  `// Function that prints minimum cost ` `// of making all elements equal ` `public` `static` `void` `solve(``int` `arr[], ``int` `n, ` `                         ``int` `a, ``int` `r, ``int` `m) ` `{ ` `     `  `    ``// Sort the given array ` `    ``Arrays.sort(arr); ` ` `  `    ``// Calcuate minimum from a + r and m ` `    ``int` `minimum = Math.min(a + r, m); ` ` `  `    ``int` `[]pref = ``new` `int` `[n + ``1``];  ` `    ``Arrays.fill(pref, ``0``); ` `     `  `    ``// Compute prefix sum and  ` `    ``// store in pref array ` `    ``for``(``int` `i = ``0``; i < n; i++) ` `       ``pref[i + ``1``] = pref[i] + arr[i]; ` ` `  `    ``int` `ans = ``10000``; ` ` `  `    ``// Find the minimum cost ` `    ``// from the given elements ` `    ``for``(``int` `i = ``0``; i < n; i++) ` `       ``ans = Math.min(ans, costCalculation(arr[i], arr,  ` `                                           ``n, pref,  ` `                                           ``a, r, minimum)); ` ` `  `    ``// Finding the minimum cost ` `    ``// from the other cases where ` `    ``// minimum cost can occur ` `    ``ans = Math.min(ans, costCalculation(pref[n] / n, arr, ` `                                        ``n, pref, a, r, ` `                                        ``minimum)); ` `    ``ans = Math.min(ans, costCalculation(pref[n] / n + ``1``,  ` `                                        ``arr, n, pref, ` `                                        ``a, r, minimum)); ` ` `  `    ``// Printing the minimum cost of making ` `    ``// all elements equal ` `    ``System.out.println(ans); ` `} ` ` `  `// Driver Code ` `public` `static` `void` `main(String args[]) ` `{ ` `    ``int` `arr[] = { ``5``, ``5``, ``3``, ``6``, ``5` `}; ` `    ``int` `A = ``1``, R = ``2``, M = ``4``; ` `    ``int` `size = arr.length ; ` ` `  `    ``// Function Call ` `    ``solve(arr, size, A, R, M); ` `} ` `} ` ` `  `// This code is contributed by SoumikMondal `

Output:

```4
```

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : SoumikMondal