human
Minimum Cost to make all array elements equal using given operations | Python3 implementation to find the minimum cost to make all array elements equal.
Checks if the value is less than middle element of the array.
Function that returns the cost of making all elements equal to current element.
Compute the lower bound of current element.
Calculate the requirement of add operation.
Calculate the requirement of subtract operation.
Compute minimum of left and right.
Computing the total cost of add and subtract operations.
Function that prints minimum cost of making all elements equal.
Sort the given array.
Calculate minimum from a + r and m.
Compute prefix sum and store in pref array.
Find the minimum cost from the given elements.
Finding the minimum cost from the other cases where minimum cost can occur.
Printing the minimum cost of making all elements equal.
Driver Code.
Function call"
How can the above be solved in Python?

What is a code to minimum Cost to make all array elements equal using given operations and the other steps required?
assistant
#Here is code to minimum Cost to make all array elements equal using given operations and the other steps required.
 def lowerBound ( array , length , value ) :
	low = 0
	high = length
	while ( low < high ) :
		mid = ( low + high ) // 2
		if ( value <= array [ mid ] ) :
			high = mid
		else :
			low = mid + 1
	return low
def costCalculation ( current , arr , n , pref , a , r , minimum ) :
	index = lowerBound ( arr , len ( arr ) , current )
	left = index * current - pref [ index ]
	right = ( pref [ n ] - pref [ index ] - ( n - index ) * current )
	res = min ( left , right )
	left -= res
	right -= res
	total = res * minimum
	total += left * a
	total += right * r
	return total
def solve ( arr , n , a , r , m ) :
	arr . sort ( )
	minimum = min ( a + r , m )
	pref = [ 0 ] * ( n + 1 )
	for i in range ( n ) :
		pref [ i + 1 ] = pref [ i ] + arr [ i ]
	ans = 10000
	for i in range ( n ) :
		ans = min ( ans , costCalculation ( arr [ i ] , arr , n , pref , a , r , minimum ) )
	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 ) )
	print ( ans )
if __name__ == " _ _ main _ _ " :
	arr = [ 5 , 5 , 3 , 6 , 5 ]
	A = 1
	R = 2
	M = 4
	size = len ( arr )
	solve ( arr , size , A , R , M )