// 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 making
// 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];
// Calculate 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);
// Calculate 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